{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# \u7b2c\u516d\u7ae0\uff1a\u6570\u636e\u7f16\u7801\u548c\u5904\u7406\n \u8fd9\u4e00\u7ae0\u4e3b\u8981\u8ba8\u8bba\u4f7f\u7528Python\u5904\u7406\u5404\u79cd\u4e0d\u540c\u65b9\u5f0f\u7f16\u7801\u7684\u6570\u636e\uff0c\u6bd4\u5982CSV\u6587\u4ef6\uff0cJSON\uff0cXML\u548c\u4e8c\u8fdb\u5236\u5305\u88c5\u8bb0\u5f55\u3002\n\u548c\u6570\u636e\u7ed3\u6784\u90a3\u4e00\u7ae0\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u7ae0\u4e0d\u4f1a\u8ba8\u8bba\u7279\u6b8a\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u800c\u662f\u5173\u6ce8\u4e8e\u600e\u6837\u83b7\u53d6\u548c\u5b58\u50a8\u8fd9\u4e9b\u683c\u5f0f\u7684\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.1 \u8bfb\u5199CSV\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e00\u4e2aCSV\u683c\u5f0f\u7684\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u591a\u6570\u7684CSV\u683c\u5f0f\u7684\u6570\u636e\u8bfb\u5199\u95ee\u9898\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528 csv \u5e93\u3002\n\u4f8b\u5982\uff1a\u5047\u8bbe\u4f60\u5728\u4e00\u4e2a\u540d\u53ebstocks.csv\u6587\u4ef6\u4e2d\u6709\u4e00\u4e9b\u80a1\u7968\u5e02\u573a\u6570\u636e\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Symbol,Price,Date,Time,Change,Volume\n\"AA\",39.48,\"6/11/2007\",\"9:36am\",-0.18,181800\n\"AIG\",71.38,\"6/11/2007\",\"9:36am\",-0.15,195500\n\"AXP\",62.58,\"6/11/2007\",\"9:36am\",-0.46,935000\n\"BA\",98.31,\"6/11/2007\",\"9:36am\",+0.12,104800\n\"C\",53.08,\"6/11/2007\",\"9:36am\",-0.25,360900\n\"CAT\",78.29,\"6/11/2007\",\"9:36am\",-0.23,225400"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u5411\u4f60\u5c55\u793a\u5982\u4f55\u5c06\u8fd9\u4e9b\u6570\u636e\u8bfb\u53d6\u4e3a\u4e00\u4e2a\u5143\u7ec4\u7684\u5e8f\u5217\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import csv\nwith open('stocks.csv') as f:\n    f_csv = csv.reader(f)\n    headers = next(f_csv)\n    for row in f_csv:\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c row \u4f1a\u662f\u4e00\u4e2a\u5217\u8868\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u8bbf\u95ee\u67d0\u4e2a\u5b57\u6bb5\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u4e0b\u6807\uff0c\u5982 row[0] \u8bbf\u95eeSymbol\uff0c row[4] \u8bbf\u95eeChange\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u8fd9\u79cd\u4e0b\u6807\u8bbf\u95ee\u901a\u5e38\u4f1a\u5f15\u8d77\u6df7\u6dc6\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u547d\u540d\u5143\u7ec4\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import namedtuple\nwith open('stock.csv') as f:\n    f_csv = csv.reader(f)\n    headings = next(f_csv)\n    Row = namedtuple('Row', headings)\n    for r in f_csv:\n        row = Row(*r)\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u5217\u540d\u5982 row.Symbol \u548c row.Change \u4ee3\u66ff\u4e0b\u6807\u8bbf\u95ee\u3002\n\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u4e2a\u53ea\u6709\u5728\u5217\u540d\u662f\u5408\u6cd5\u7684Python\u6807\u8bc6\u7b26\u7684\u65f6\u5019\u624d\u751f\u6548\u3002\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\n\u4f60\u53ef\u80fd\u9700\u8981\u4fee\u6539\u4e0b\u539f\u59cb\u7684\u5217\u540d(\u5982\u5c06\u975e\u6807\u8bc6\u7b26\u5b57\u7b26\u66ff\u6362\u6210\u4e0b\u5212\u7ebf\u4e4b\u7c7b\u7684)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u4e00\u4e2a\u9009\u62e9\u5c31\u662f\u5c06\u6570\u636e\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u5178\u5e8f\u5217\u4e2d\u53bb\u3002\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import csv\nwith open('stocks.csv') as f:\n    f_csv = csv.DictReader(f)\n    for row in f_csv:\n        # process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u540d\u53bb\u8bbf\u95ee\u6bcf\u4e00\u884c\u7684\u6570\u636e\u4e86\u3002\u6bd4\u5982\uff0crow['Symbol'] \u6216\u8005 row['Change']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5199\u5165CSV\u6570\u636e\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528csv\u6a21\u5757\uff0c\u4e0d\u8fc7\u8fd9\u65f6\u5019\u5148\u521b\u5efa\u4e00\u4e2a writer \u5bf9\u8c61\u3002\u4f8b\u5982:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "headers = ['Symbol','Price','Date','Time','Change','Volume']\nrows = [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),\n         ('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),\n         ('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),\n       ]\n\nwith open('stocks.csv','w') as f:\n    f_csv = csv.writer(f)\n    f_csv.writerow(headers)\n    f_csv.writerows(rows)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\u5e8f\u5217\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u50cf\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']\nrows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.18, 'Volume':181800},\n        {'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},\n        {'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',\n        'Time':'9:36am', 'Change':-0.46, 'Volume': 935000},\n        ]\n\nwith open('stocks.csv','w') as f:\n    f_csv = csv.DictWriter(f, headers)\n    f_csv.writeheader()\n    f_csv.writerows(rows)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e94\u8be5\u603b\u662f\u4f18\u5148\u9009\u62e9csv\u6a21\u5757\u5206\u5272\u6216\u89e3\u6790CSV\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u50cf\u7f16\u5199\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('stocks.csv') as f:\nfor line in f:\n    row = line.split(',')\n    # process row\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u4f60\u4ecd\u7136\u9700\u8981\u53bb\u5904\u7406\u4e00\u4e9b\u68d8\u624b\u7684\u7ec6\u8282\u95ee\u9898\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u67d0\u4e9b\u5b57\u6bb5\u503c\u88ab\u5f15\u53f7\u5305\u56f4\uff0c\u4f60\u4e0d\u5f97\u4e0d\u53bb\u9664\u8fd9\u4e9b\u5f15\u53f7\u3002\n\u53e6\u5916\uff0c\u5982\u679c\u4e00\u4e2a\u88ab\u5f15\u53f7\u5305\u56f4\u7684\u5b57\u6bb5\u78b0\u5de7\u542b\u6709\u4e00\u4e2a\u9017\u53f7\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u4f1a\u56e0\u4e3a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u5927\u5c0f\u7684\u884c\u800c\u51fa\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ccsv \u5e93\u53ef\u8bc6\u522bMicrosoft Excel\u6240\u4f7f\u7528\u7684CSV\u7f16\u7801\u89c4\u5219\u3002\n\u8fd9\u6216\u8bb8\u4e5f\u662f\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\uff0c\u5e76\u4e14\u4e5f\u4f1a\u7ed9\u4f60\u5e26\u6765\u6700\u597d\u7684\u517c\u5bb9\u6027\u3002\n\u7136\u800c\uff0c\u5982\u679c\u4f60\u67e5\u770bcsv\u7684\u6587\u6863\uff0c\u5c31\u4f1a\u53d1\u73b0\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u5c06\u5b83\u5e94\u7528\u5230\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u4e0a(\u5982\u4fee\u6539\u5206\u5272\u5b57\u7b26\u7b49)\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8bfb\u53d6\u4ee5tab\u5206\u5272\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example of reading tab-separated values\nwith open('stock.tsv') as f:\n    f_tsv = csv.reader(f, delimiter='\\t')\n    for row in f_tsv:\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6b63\u5728\u8bfb\u53d6CSV\u6570\u636e\u5e76\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u547d\u540d\u5143\u7ec4\uff0c\u9700\u8981\u6ce8\u610f\u5bf9\u5217\u540d\u8fdb\u884c\u5408\u6cd5\u6027\u8ba4\u8bc1\u3002\n\u4f8b\u5982\uff0c\u4e00\u4e2aCSV\u683c\u5f0f\u6587\u4ef6\u6709\u4e00\u4e2a\u5305\u542b\u975e\u6cd5\u6807\u8bc6\u7b26\u7684\u5217\u5934\u884c\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Street\u00a0Address,Num-Premises,Latitude,Longitude 5412\u00a0N\u00a0CLARK,10,41.980262,-87.668452"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u6700\u7ec8\u4f1a\u5bfc\u81f4\u5728\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u65f6\u4ea7\u751f\u4e00\u4e2a ValueError \u5f02\u5e38\u800c\u5931\u8d25\u3002\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u95ee\u9898\uff0c\u4f60\u53ef\u80fd\u4e0d\u5f97\u4e0d\u5148\u53bb\u4fee\u6b63\u5217\u6807\u9898\u3002\n\u4f8b\u5982\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5728\u975e\u6cd5\u6807\u8bc6\u7b26\u4e0a\u4f7f\u7528\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import re\nwith open('stock.csv') as f:\n    f_csv = csv.reader(f)\n    headers = [ re.sub('[^a-zA-Z_]', '_', h) for h in next(f_csv) ]\n    Row = namedtuple('Row', headers)\n    for r in f_csv:\n        row = Row(*r)\n        # Process row\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u91cd\u8981\u7684\u4e00\u70b9\u9700\u8981\u5f3a\u8c03\u7684\u662f\uff0ccsv\u4ea7\u751f\u7684\u6570\u636e\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\uff0c\u5b83\u4e0d\u4f1a\u505a\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\n\u5982\u679c\u4f60\u9700\u8981\u505a\u8fd9\u6837\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4f60\u5fc5\u987b\u81ea\u5df1\u624b\u52a8\u53bb\u5b9e\u73b0\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5728CSV\u6570\u636e\u4e0a\u6267\u884c\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "col_types = [str, float, str, str, float, int]\nwith open('stocks.csv') as f:\n    f_csv = csv.reader(f)\n    headers = next(f_csv)\n    for row in f_csv:\n        # Apply conversions to the row items\n        row = tuple(convert(value) for convert, value in zip(col_types, row))\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u8f6c\u6362\u5b57\u5178\u4e2d\u7279\u5b9a\u5b57\u6bb5\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('Reading as dicts with type conversion')\nfield_types = [ ('Price', float),\n                ('Change', float),\n                ('Volume', int) ]\n\nwith open('stocks.csv') as f:\n    for row in csv.DictReader(f):\n        row.update((key, conversion(row[key]))\n                for key, conversion in field_types)\n        print(row)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4f60\u53ef\u80fd\u5e76\u4e0d\u60f3\u8fc7\u591a\u53bb\u8003\u8651\u8fd9\u4e9b\u8f6c\u6362\u95ee\u9898\u3002\n\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0cCSV\u6587\u4ef6\u90fd\u6216\u591a\u6216\u5c11\u6709\u4e9b\u7f3a\u5931\u7684\u6570\u636e\uff0c\u88ab\u7834\u574f\u7684\u6570\u636e\u4ee5\u53ca\u5176\u5b83\u4e00\u4e9b\u8ba9\u8f6c\u6362\u5931\u8d25\u7684\u95ee\u9898\u3002\n\u56e0\u6b64\uff0c\u9664\u975e\u4f60\u7684\u6570\u636e\u786e\u5b9e\u6709\u4fdd\u969c\u662f\u51c6\u786e\u65e0\u8bef\u7684\uff0c\u5426\u5219\u4f60\u5fc5\u987b\u8003\u8651\u8fd9\u4e9b\u95ee\u9898(\u4f60\u53ef\u80fd\u9700\u8981\u589e\u52a0\u5408\u9002\u7684\u9519\u8bef\u5904\u7406\u673a\u5236)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5982\u679c\u4f60\u8bfb\u53d6CSV\u6570\u636e\u7684\u76ee\u7684\u662f\u505a\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u7684\u8bdd\uff0c\n\u4f60\u53ef\u80fd\u9700\u8981\u770b\u4e00\u770b Pandas \u5305\u3002Pandas \u5305\u542b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570\u53eb pandas.read_csv() \uff0c\n\u5b83\u53ef\u4ee5\u52a0\u8f7dCSV\u6570\u636e\u5230\u4e00\u4e2a DataFrame \u5bf9\u8c61\u4e2d\u53bb\u3002\n\u7136\u540e\u5229\u7528\u8fd9\u4e2a\u5bf9\u8c61\u4f60\u5c31\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u5f62\u5f0f\u7684\u7edf\u8ba1\u3001\u8fc7\u6ee4\u6570\u636e\u4ee5\u53ca\u6267\u884c\u5176\u4ed6\u9ad8\u7ea7\u64cd\u4f5c\u4e86\u3002\n\u57286.13\u5c0f\u8282\u4e2d\u4f1a\u6709\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.2 \u8bfb\u5199JSON\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199JSON(JavaScript Object Notation)\u7f16\u7801\u683c\u5f0f\u7684\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "json \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u7f16\u7801\u548c\u89e3\u7801JSON\u6570\u636e\u3002\n\u5176\u4e2d\u4e24\u4e2a\u4e3b\u8981\u7684\u51fd\u6570\u662f json.dumps() \u548c json.loads() \uff0c\n\u8981\u6bd4\u5176\u4ed6\u5e8f\u5217\u5316\u51fd\u6570\u5e93\u5982pickle\u7684\u63a5\u53e3\u5c11\u5f97\u591a\u3002\n\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u5c06\u4e00\u4e2aPython\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import json\n\ndata = {\n    'name' : 'ACME',\n    'shares' : 100,\n    'price' : 542.23\n}\n\njson_str = json.dumps(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u5c06\u4e00\u4e2aJSON\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u4e00\u4e2aPython\u6570\u636e\u7ed3\u6784\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = json.loads(json_str)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8981\u5904\u7406\u7684\u662f\u6587\u4ef6\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 json.dump() \u548c json.load() \u6765\u7f16\u7801\u548c\u89e3\u7801JSON\u6570\u636e\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Writing JSON data\nwith open('data.json', 'w') as f:\n    json.dump(data, f)\n\n# Reading data back\nwith open('data.json', 'r') as f:\n    data = json.load(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "JSON\u7f16\u7801\u652f\u6301\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e3a None \uff0c bool \uff0c int \uff0c float \u548c str \uff0c\n\u4ee5\u53ca\u5305\u542b\u8fd9\u4e9b\u7c7b\u578b\u6570\u636e\u7684lists\uff0ctuples\u548cdictionaries\u3002\n\u5bf9\u4e8edictionaries\uff0ckeys\u9700\u8981\u662f\u5b57\u7b26\u4e32\u7c7b\u578b(\u5b57\u5178\u4e2d\u4efb\u4f55\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684key\u5728\u7f16\u7801\u65f6\u4f1a\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32)\u3002\n\u4e3a\u4e86\u9075\u5faaJSON\u89c4\u8303\uff0c\u4f60\u5e94\u8be5\u53ea\u7f16\u7801Python\u7684lists\u548cdictionaries\u3002\n\u800c\u4e14\uff0c\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u9876\u5c42\u5bf9\u8c61\u88ab\u7f16\u7801\u4e3a\u4e00\u4e2a\u5b57\u5178\u662f\u4e00\u4e2a\u6807\u51c6\u505a\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "JSON\u7f16\u7801\u7684\u683c\u5f0f\u5bf9\u4e8ePython\u8bed\u6cd5\u800c\u5df2\u51e0\u4e4e\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u9664\u4e86\u4e00\u4e9b\u5c0f\u7684\u5dee\u5f02\u4e4b\u5916\u3002\n\u6bd4\u5982\uff0cTrue\u4f1a\u88ab\u6620\u5c04\u4e3atrue\uff0cFalse\u88ab\u6620\u5c04\u4e3afalse\uff0c\u800cNone\u4f1a\u88ab\u6620\u5c04\u4e3anull\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u7f16\u7801\u540e\u7684\u5b57\u7b26\u4e32\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "json.dumps(False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = {'a': True,\n    'b': 'Hello',\n    'c': None}\njson.dumps(d)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u7740\u53bb\u68c0\u67e5JSON\u89e3\u7801\u540e\u7684\u6570\u636e\uff0c\u4f60\u901a\u5e38\u5f88\u96be\u901a\u8fc7\u7b80\u5355\u7684\u6253\u5370\u6765\u786e\u5b9a\u5b83\u7684\u7ed3\u6784\uff0c\n\u7279\u522b\u662f\u5f53\u6570\u636e\u7684\u5d4c\u5957\u7ed3\u6784\u5c42\u6b21\u5f88\u6df1\u6216\u8005\u5305\u542b\u5927\u91cf\u7684\u5b57\u6bb5\u65f6\u3002\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528pprint\u6a21\u5757\u7684 pprint() \u51fd\u6570\u6765\u4ee3\u66ff\u666e\u901a\u7684 print() \u51fd\u6570\u3002\n\u5b83\u4f1a\u6309\u7167key\u7684\u5b57\u6bcd\u987a\u5e8f\u5e76\u4ee5\u4e00\u79cd\u66f4\u52a0\u7f8e\u89c2\u7684\u65b9\u5f0f\u8f93\u51fa\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u6f14\u793a\u5982\u4f55\u6f02\u4eae\u7684\u6253\u5370\u8f93\u51faTwitter\u4e0a\u641c\u7d22\u7ed3\u679c\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib.request import urlopen\nimport json\nu = urlopen('http://search.twitter.com/search.json?q=python&rpp=5')\nresp = json.loads(u.read().decode('utf-8'))\nfrom pprint import pprint\npprint(resp)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0cJSON\u89e3\u7801\u4f1a\u6839\u636e\u63d0\u4f9b\u7684\u6570\u636e\u521b\u5efadicts\u6216lists\u3002\n\u5982\u679c\u4f60\u60f3\u8981\u521b\u5efa\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7ed9 json.loads() \u4f20\u9012object_pairs_hook\u6216object_hook\u53c2\u6570\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u6f14\u793a\u5982\u4f55\u89e3\u7801JSON\u6570\u636e\u5e76\u5728\u4e00\u4e2aOrderedDict\u4e2d\u4fdd\u7559\u5176\u987a\u5e8f\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = '{\"name\": \"ACME\", \"shares\": 50, \"price\": 490.1}'\nfrom collections import OrderedDict\ndata = json.loads(s, object_pairs_hook=OrderedDict)\ndata"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u5982\u4f55\u5c06\u4e00\u4e2aJSON\u5b57\u5178\u8f6c\u6362\u4e3a\u4e00\u4e2aPython\u5bf9\u8c61\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class JSONObject:\n    def __init__(self, d):\n        self.__dict__ = d\ndata = json.loads(s, object_hook=JSONObject)\ndata.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data.shares"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data.price"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0cJSON\u89e3\u7801\u540e\u7684\u5b57\u5178\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9 __init__() \u3002\n\u7136\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u7684\u4f7f\u7528\u5b83\u4e86\uff0c\u6bd4\u5982\u4f5c\u4e3a\u4e00\u4e2a\u5b9e\u4f8b\u5b57\u5178\u6765\u76f4\u63a5\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7f16\u7801JSON\u7684\u65f6\u5019\uff0c\u8fd8\u6709\u4e00\u4e9b\u9009\u9879\u5f88\u6709\u7528\u3002\n\u5982\u679c\u4f60\u60f3\u83b7\u5f97\u6f02\u4eae\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u540e\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528 json.dumps() \u7684indent\u53c2\u6570\u3002\n\u5b83\u4f1a\u4f7f\u5f97\u8f93\u51fa\u548cpprint()\u51fd\u6570\u6548\u679c\u7c7b\u4f3c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(json.dumps(data))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(json.dumps(data, indent=4))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u8c61\u5b9e\u4f8b\u901a\u5e38\u5e76\u4e0d\u662fJSON\u53ef\u5e8f\u5217\u5316\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\np = Point(2, 3)\njson.dumps(p)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5e8f\u5217\u5316\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u4f60\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u7684\u8f93\u5165\u662f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd4\u56de\u4e00\u4e2a\u53ef\u5e8f\u5217\u5316\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def serialize_instance(obj):\n    d = { '__classname__' : type(obj).__name__ }\n    d.update(vars(obj))\n    return d"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u53cd\u8fc7\u6765\u83b7\u53d6\u8fd9\u4e2a\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Dictionary mapping names to known classes\nclasses = {\n    'Point' : Point\n}\n\ndef unserialize_object(d):\n    clsname = d.pop('__classname__', None)\n    if clsname:\n        cls = classes[clsname]\n        obj = cls.__new__(cls) # Make instance without calling __init__\n        for key, value in d.items():\n            setattr(obj, key, value)\n        return obj\n    else:\n        return d"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Point(2,3)\ns = json.dumps(p, default=serialize_instance)\ns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = json.loads(s, object_hook=unserialize_object)\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a.y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "json \u6a21\u5757\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u9009\u9879\u6765\u63a7\u5236\u66f4\u4f4e\u7ea7\u522b\u7684\u6570\u5b57\u3001\u7279\u6b8a\u503c\u5982NaN\u7b49\u7684\u89e3\u6790\u3002\n\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\u83b7\u53d6\u66f4\u591a\u7ec6\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.3 \u89e3\u6790\u7b80\u5355\u7684XML\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4ece\u4e00\u4e2a\u7b80\u5355\u7684XML\u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u4f7f\u7528 xml.etree.ElementTree \u6a21\u5757\u4ece\u7b80\u5355\u7684XML\u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u3002\n\u4e3a\u4e86\u6f14\u793a\uff0c\u5047\u8bbe\u4f60\u60f3\u89e3\u6790Planet Python\u4e0a\u7684RSS\u6e90\u3002\u4e0b\u9762\u662f\u76f8\u5e94\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib.request import urlopen\nfrom xml.etree.ElementTree import parse\n\n# Download the RSS feed and parse it\nu = urlopen('http://planet.python.org/rss20.xml')\ndoc = parse(u)\n\n# Extract and output tags of interest\nfor item in doc.iterfind('channel/item'):\n    title = item.findtext('title')\n    date = item.findtext('pubDate')\n    link = item.findtext('link')\n\n    print(title)\n    print(date)\n    print(link)\n    print()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u7c7b\u4f3c\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Steve Holden: Python for Data Analysis\nMon, 19 Nov 2012 02:13:51 +0000\nhttp://holdenweb.blogspot.com/2012/11/python-for-data-analysis.html\n\nVasudev Ram: The Python Data model (for v2 and v3)\nSun, 18 Nov 2012 22:06:47 +0000\nhttp://jugad2.blogspot.com/2012/11/the-python-data-model.html\n\nPython Diary: Been playing around with Object Databases\nSun, 18 Nov 2012 20:40:29 +0000\nhttp://www.pythondiary.com/blog/Nov.18,2012/been-...-object-databases.html\n\nVasudev Ram: Wakari, Scientific Python in the cloud\nSun, 18 Nov 2012 20:19:41 +0000\nhttp://jugad2.blogspot.com/2012/11/wakari-scientific-python-in-cloud.html\n\nJesse Jiryu Davis: Toro: synchronization primitives for Tornado coroutines\nSun, 18 Nov 2012 20:17:49 +0000\nhttp://feedproxy.google.com/~r/EmptysquarePython/~3/_DOZT2Kd0hQ/"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u663e\u7136\uff0c\u5982\u679c\u4f60\u60f3\u505a\u8fdb\u4e00\u6b65\u7684\u5904\u7406\uff0c\u4f60\u9700\u8981\u66ff\u6362 print() \u8bed\u53e5\u6765\u5b8c\u6210\u5176\u4ed6\u6709\u8da3\u7684\u4e8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406XML\u7f16\u7801\u683c\u5f0f\u7684\u6570\u636e\u662f\u5f88\u5e38\u89c1\u7684\u3002\n\u4e0d\u4ec5\u56e0\u4e3aXML\u5728Internet\u4e0a\u9762\u5df2\u7ecf\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u4ea4\u6362\uff0c\n\u540c\u65f6\u5b83\u4e5f\u662f\u4e00\u79cd\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u6570\u636e\u7684\u5e38\u7528\u683c\u5f0f(\u6bd4\u5982\u5b57\u5904\u7406\uff0c\u97f3\u4e50\u5e93\u7b49)\u3002\n\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4f1a\u5148\u5047\u5b9a\u8bfb\u8005\u5df2\u7ecf\u5bf9XML\u57fa\u7840\u6bd4\u8f83\u719f\u6089\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u5f53\u4f7f\u7528XML\u6765\u4ec5\u4ec5\u5b58\u50a8\u6570\u636e\u7684\u65f6\u5019\uff0c\u5bf9\u5e94\u7684\u6587\u6863\u7ed3\u6784\u975e\u5e38\u7d27\u51d1\u5e76\u4e14\u76f4\u89c2\u3002\n\u4f8b\u5982\uff0c\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684RSS\u8ba2\u9605\u6e90\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u7684\u683c\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "<?xml version=\"1.0\"?>\n<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n    <channel>\n        <title>Planet Python</title>\n        <link>http://planet.python.org/</link>\n        <language>en</language>\n        <description>Planet Python - http://planet.python.org/</description>\n        <item>\n            <title>Steve Holden: Python for Data Analysis</title>\n            <guid>http://holdenweb.blogspot.com/...-data-analysis.html</guid>\n            <link>http://holdenweb.blogspot.com/...-data-analysis.html</link>\n            <description>...</description>\n            <pubDate>Mon, 19 Nov 2012 02:13:51 +0000</pubDate>\n        </item>\n        <item>\n            <title>Vasudev Ram: The Python Data model (for v2 and v3)</title>\n            <guid>http://jugad2.blogspot.com/...-data-model.html</guid>\n            <link>http://jugad2.blogspot.com/...-data-model.html</link>\n            <description>...</description>\n            <pubDate>Sun, 18 Nov 2012 22:06:47 +0000</pubDate>\n        </item>\n        <item>\n            <title>Python Diary: Been playing around with Object Databases</title>\n            <guid>http://www.pythondiary.com/...-object-databases.html</guid>\n            <link>http://www.pythondiary.com/...-object-databases.html</link>\n            <description>...</description>\n            <pubDate>Sun, 18 Nov 2012 20:40:29 +0000</pubDate>\n        </item>\n        ...\n    </channel>\n</rss>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "xml.etree.ElementTree.parse() \u51fd\u6570\u89e3\u6790\u6574\u4e2aXML\u6587\u6863\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u6587\u6863\u5bf9\u8c61\u3002\n\u7136\u540e\uff0c\u4f60\u5c31\u80fd\u4f7f\u7528 find() \u3001iterfind() \u548c findtext() \u7b49\u65b9\u6cd5\u6765\u641c\u7d22\u7279\u5b9a\u7684XML\u5143\u7d20\u4e86\u3002\n\u8fd9\u4e9b\u51fd\u6570\u7684\u53c2\u6570\u5c31\u662f\u67d0\u4e2a\u6307\u5b9a\u7684\u6807\u7b7e\u540d\uff0c\u4f8b\u5982 channel/item \u6216 title \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bcf\u6b21\u6307\u5b9a\u67d0\u4e2a\u6807\u7b7e\u65f6\uff0c\u4f60\u9700\u8981\u904d\u5386\u6574\u4e2a\u6587\u6863\u7ed3\u6784\u3002\u6bcf\u6b21\u641c\u7d22\u64cd\u4f5c\u4f1a\u4ece\u4e00\u4e2a\u8d77\u59cb\u5143\u7d20\u5f00\u59cb\u8fdb\u884c\u3002\n\u540c\u6837\uff0c\u6bcf\u6b21\u64cd\u4f5c\u6240\u6307\u5b9a\u7684\u6807\u7b7e\u540d\u4e5f\u662f\u8d77\u59cb\u5143\u7d20\u7684\u76f8\u5bf9\u8def\u5f84\u3002\n\u4f8b\u5982\uff0c\u6267\u884c doc.iterfind('channel/item') \u6765\u641c\u7d22\u6240\u6709\u5728 channel \u5143\u7d20\u4e0b\u9762\u7684 item \u5143\u7d20\u3002\ndoc \u4ee3\u8868\u6587\u6863\u7684\u6700\u9876\u5c42(\u4e5f\u5c31\u662f\u7b2c\u4e00\u7ea7\u7684 rss \u5143\u7d20)\u3002\n\u7136\u540e\u63a5\u4e0b\u6765\u7684\u8c03\u7528 item.findtext() \u4f1a\u4ece\u5df2\u627e\u5230\u7684 item \u5143\u7d20\u4f4d\u7f6e\u5f00\u59cb\u641c\u7d22\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "ElementTree \u6a21\u5757\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5728\u89e3\u6790\u7684\u65f6\u5019\u975e\u5e38\u6709\u7528\u3002\ntag \u5c5e\u6027\u5305\u542b\u4e86\u6807\u7b7e\u7684\u540d\u5b57\uff0ctext \u5c5e\u6027\u5305\u542b\u4e86\u5185\u90e8\u7684\u6587\u672c\uff0c\u800c get() \u65b9\u6cd5\u80fd\u83b7\u53d6\u5c5e\u6027\u503c\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "doc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e = doc.find('channel/title')\ne"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e.tag"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e.text"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e.get('some_attribute')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e00\u70b9\u8981\u5f3a\u8c03\u7684\u662f xml.etree.ElementTree \u5e76\u4e0d\u662fXML\u89e3\u6790\u7684\u552f\u4e00\u65b9\u6cd5\u3002\n\u5bf9\u4e8e\u66f4\u9ad8\u7ea7\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u9700\u8981\u8003\u8651\u4f7f\u7528 lxml \u3002\n\u5b83\u4f7f\u7528\u4e86\u548cElementTree\u540c\u6837\u7684\u7f16\u7a0b\u63a5\u53e3\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u4f8b\u5b50\u540c\u6837\u4e5f\u9002\u7528\u4e8elxml\u3002\n\u4f60\u53ea\u9700\u8981\u5c06\u521a\u5f00\u59cb\u7684import\u8bed\u53e5\u6362\u6210 from lxml.etree import parse \u5c31\u884c\u4e86\u3002\nlxml \u5b8c\u5168\u9075\u5faaXML\u6807\u51c6\uff0c\u5e76\u4e14\u901f\u5ea6\u4e5f\u975e\u5e38\u5feb\uff0c\u540c\u65f6\u8fd8\u652f\u6301\u9a8c\u8bc1\uff0cXSLT\uff0c\u548cXPath\u7b49\u7279\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.4 \u589e\u91cf\u5f0f\u89e3\u6790\u5927\u578bXML\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528\u5c3d\u53ef\u80fd\u5c11\u7684\u5185\u5b58\u4ece\u4e00\u4e2a\u8d85\u5927\u7684XML\u6587\u6863\u4e2d\u63d0\u53d6\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u4f60\u9047\u5230\u589e\u91cf\u5f0f\u7684\u6570\u636e\u5904\u7406\u65f6\uff0c\u7b2c\u4e00\u65f6\u95f4\u5c31\u5e94\u8be5\u60f3\u5230\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u51fd\u6570\uff0c\u53ea\u4f7f\u7528\u5f88\u5c11\u7684\u5185\u5b58\u5c31\u80fd\u589e\u91cf\u5f0f\u7684\u5904\u7406\u4e00\u4e2a\u5927\u578bXML\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import iterparse\n\ndef parse_and_remove(filename, path):\n    path_parts = path.split('/')\n    doc = iterparse(filename, ('start', 'end'))\n    # Skip the root element\n    next(doc)\n\n    tag_stack = []\n    elem_stack = []\n    for event, elem in doc:\n        if event == 'start':\n            tag_stack.append(elem.tag)\n            elem_stack.append(elem)\n        elif event == 'end':\n            if tag_stack == path_parts:\n                yield elem\n                elem_stack[-2].remove(elem)\n            try:\n                tag_stack.pop()\n                elem_stack.pop()\n            except IndexError:\n                pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u9700\u8981\u5148\u6709\u4e00\u4e2a\u5927\u578b\u7684XML\u6587\u4ef6\u3002\n\u901a\u5e38\u4f60\u53ef\u4ee5\u5728\u653f\u5e9c\u7f51\u7ad9\u6216\u516c\u5171\u6570\u636e\u7f51\u7ad9\u4e0a\u627e\u5230\u8fd9\u6837\u7684\u6587\u4ef6\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4e0b\u8f7dXML\u683c\u5f0f\u7684\u829d\u52a0\u54e5\u57ce\u5e02\u9053\u8def\u5751\u6d3c\u6570\u636e\u5e93\u3002\n\u5728\u5199\u8fd9\u672c\u4e66\u7684\u65f6\u5019\uff0c\u4e0b\u8f7d\u6587\u4ef6\u5df2\u7ecf\u5305\u542b\u8d85\u8fc7100,000\u884c\u6570\u636e\uff0c\u7f16\u7801\u683c\u5f0f\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "<response>\n    <row>\n        <row ...>\n            <creation_date>2012-11-18T00:00:00</creation_date>\n            <status>Completed</status>\n            <completion_date>2012-11-18T00:00:00</completion_date>\n            <service_request_number>12-01906549</service_request_number>\n            <type_of_service_request>Pot Hole in Street</type_of_service_request>\n            <current_activity>Final Outcome</current_activity>\n            <most_recent_action>CDOT Street Cut ... Outcome</most_recent_action>\n            <street_address>4714 S TALMAN AVE</street_address>\n            <zip>60632</zip>\n            <x_coordinate>1159494.68618856</x_coordinate>\n            <y_coordinate>1873313.83503384</y_coordinate>\n            <ward>14</ward>\n            <police_district>9</police_district>\n            <community_area>58</community_area>\n            <latitude>41.808090232127896</latitude>\n            <longitude>-87.69053684711305</longitude>\n            <location latitude=\"41.808090232127896\"\n            longitude=\"-87.69053684711305\" />\n        </row>\n        <row ...>\n            <creation_date>2012-11-18T00:00:00</creation_date>\n            <status>Completed</status>\n            <completion_date>2012-11-18T00:00:00</completion_date>\n            <service_request_number>12-01906695</service_request_number>\n            <type_of_service_request>Pot Hole in Street</type_of_service_request>\n            <current_activity>Final Outcome</current_activity>\n            <most_recent_action>CDOT Street Cut ... Outcome</most_recent_action>\n            <street_address>3510 W NORTH AVE</street_address>\n            <zip>60647</zip>\n            <x_coordinate>1152732.14127696</x_coordinate>\n            <y_coordinate>1910409.38979075</y_coordinate>\n            <ward>26</ward>\n            <police_district>14</police_district>\n            <community_area>23</community_area>\n            <latitude>41.91002084292946</latitude>\n            <longitude>-87.71435952353961</longitude>\n            <location latitude=\"41.91002084292946\"\n            longitude=\"-87.71435952353961\" />\n        </row>\n    </row>\n</response>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u4f60\u60f3\u5199\u4e00\u4e2a\u811a\u672c\u6765\u6309\u7167\u5751\u6d3c\u62a5\u544a\u6570\u91cf\u6392\u5217\u90ae\u7f16\u53f7\u7801\u3002\u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import parse\nfrom collections import Counter\n\npotholes_by_zip = Counter()\n\ndoc = parse('potholes.xml')\nfor pothole in doc.iterfind('row/row'):\n    potholes_by_zip[pothole.findtext('zip')] += 1\nfor zipcode, num in potholes_by_zip.most_common():\n    print(zipcode, num)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u811a\u672c\u552f\u4e00\u7684\u95ee\u9898\u662f\u5b83\u4f1a\u5148\u5c06\u6574\u4e2aXML\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7136\u540e\u89e3\u6790\u3002\n\u5728\u6211\u7684\u673a\u5668\u4e0a\uff0c\u4e3a\u4e86\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u9700\u8981\u7528\u5230450MB\u5de6\u53f3\u7684\u5185\u5b58\u7a7a\u95f4\u3002\n\u5982\u679c\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff0c\u7a0b\u5e8f\u53ea\u9700\u8981\u4fee\u6539\u4e00\u70b9\u70b9\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import Counter\n\npotholes_by_zip = Counter()\n\ndata = parse_and_remove('potholes.xml', 'row/row')\nfor pothole in data:\n    potholes_by_zip[pothole.findtext('zip')] += 1\nfor zipcode, num in potholes_by_zip.most_common():\n    print(zipcode, num)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u662f\uff1a\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\u8fd0\u884c\u65f6\u53ea\u9700\u89817MB\u7684\u5185\u5b58\u2013\u5927\u5927\u8282\u7ea6\u4e86\u5185\u5b58\u8d44\u6e90\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u8282\u7684\u6280\u672f\u4f1a\u4f9d\u8d56 ElementTree \u6a21\u5757\u4e2d\u7684\u4e24\u4e2a\u6838\u5fc3\u529f\u80fd\u3002\n\u7b2c\u4e00\uff0citerparse() \u65b9\u6cd5\u5141\u8bb8\u5bf9XML\u6587\u6863\u8fdb\u884c\u589e\u91cf\u64cd\u4f5c\u3002\n\u4f7f\u7528\u65f6\uff0c\u4f60\u9700\u8981\u63d0\u4f9b\u6587\u4ef6\u540d\u548c\u4e00\u4e2a\u5305\u542b\u4e0b\u9762\u4e00\u79cd\u6216\u591a\u79cd\u7c7b\u578b\u7684\u4e8b\u4ef6\u5217\u8868\uff1a\nstart , end, start-ns \u548c end-ns \u3002\n\u7531 iterparse() \u521b\u5efa\u7684\u8fed\u4ee3\u5668\u4f1a\u4ea7\u751f\u5f62\u5982 (event, elem) \u7684\u5143\u7ec4\uff0c\n\u5176\u4e2d event \u662f\u4e0a\u8ff0\u4e8b\u4ef6\u5217\u8868\u4e2d\u7684\u67d0\u4e00\u4e2a\uff0c\u800c elem \u662f\u76f8\u5e94\u7684XML\u5143\u7d20\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = iterparse('potholes.xml',('start','end'))\nnext(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "start \u4e8b\u4ef6\u5728\u67d0\u4e2a\u5143\u7d20\u7b2c\u4e00\u6b21\u88ab\u521b\u5efa\u5e76\u4e14\u8fd8\u6ca1\u6709\u88ab\u63d2\u5165\u5176\u4ed6\u6570\u636e(\u5982\u5b50\u5143\u7d20)\u65f6\u88ab\u521b\u5efa\u3002\n\u800c end \u4e8b\u4ef6\u5728\u67d0\u4e2a\u5143\u7d20\u5df2\u7ecf\u5b8c\u6210\u65f6\u88ab\u521b\u5efa\u3002\n\u5c3d\u7ba1\u6ca1\u6709\u5728\u4f8b\u5b50\u4e2d\u6f14\u793a\uff0c start-ns \u548c end-ns \u4e8b\u4ef6\u88ab\u7528\u6765\u5904\u7406XML\u6587\u6863\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u672c\u8282\u4f8b\u5b50\u4e2d\uff0c start \u548c end \u4e8b\u4ef6\u88ab\u7528\u6765\u7ba1\u7406\u5143\u7d20\u548c\u6807\u7b7e\u6808\u3002\n\u6808\u4ee3\u8868\u4e86\u6587\u6863\u88ab\u89e3\u6790\u65f6\u7684\u5c42\u6b21\u7ed3\u6784\uff0c\n\u8fd8\u88ab\u7528\u6765\u5224\u65ad\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5339\u914d\u4f20\u7ed9\u51fd\u6570 parse_and_remove() \u7684\u8def\u5f84\u3002\n\u5982\u679c\u5339\u914d\uff0c\u5c31\u5229\u7528 yield \u8bed\u53e5\u5411\u8c03\u7528\u8005\u8fd4\u56de\u8fd9\u4e2a\u5143\u7d20\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 yield \u4e4b\u540e\u7684\u4e0b\u9762\u8fd9\u4e2a\u8bed\u53e5\u624d\u662f\u4f7f\u5f97\u7a0b\u5e8f\u5360\u7528\u6781\u5c11\u5185\u5b58\u7684ElementTree\u7684\u6838\u5fc3\u7279\u6027\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "elem_stack[-2].remove(elem)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u8bed\u53e5\u4f7f\u5f97\u4e4b\u524d\u7531 yield \u4ea7\u751f\u7684\u5143\u7d20\u4ece\u5b83\u7684\u7236\u8282\u70b9\u4e2d\u5220\u9664\u6389\u3002\n\u5047\u8bbe\u5df2\u7ecf\u6ca1\u6709\u5176\u5b83\u7684\u5730\u65b9\u5f15\u7528\u8fd9\u4e2a\u5143\u7d20\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5143\u7d20\u5c31\u88ab\u9500\u6bc1\u5e76\u56de\u6536\u5185\u5b58\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u8282\u70b9\u7684\u8fed\u4ee3\u5f0f\u89e3\u6790\u548c\u5220\u9664\u7684\u6700\u7ec8\u6548\u679c\u5c31\u662f\u4e00\u4e2a\u5728\u6587\u6863\u4e0a\u9ad8\u6548\u7684\u589e\u91cf\u5f0f\u6e05\u626b\u8fc7\u7a0b\u3002\n\u6587\u6863\u6811\u7ed3\u6784\u4ece\u59cb\u81ea\u7ec8\u6ca1\u88ab\u5b8c\u6574\u7684\u521b\u5efa\u8fc7\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8fd8\u662f\u80fd\u901a\u8fc7\u4e0a\u8ff0\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd9\u4e2aXML\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u7684\u4e3b\u8981\u7f3a\u9677\u5c31\u662f\u5b83\u7684\u8fd0\u884c\u6027\u80fd\u4e86\u3002\n\u6211\u81ea\u5df1\u6d4b\u8bd5\u7684\u7ed3\u679c\u662f\uff0c\u8bfb\u53d6\u6574\u4e2a\u6587\u6863\u5230\u5185\u5b58\u4e2d\u7684\u7248\u672c\u7684\u8fd0\u884c\u901f\u5ea6\u5dee\u4e0d\u591a\u662f\u589e\u91cf\u5f0f\u5904\u7406\u7248\u672c\u7684\u4e24\u500d\u5feb\u3002\n\u4f46\u662f\u5b83\u5374\u4f7f\u7528\u4e86\u8d85\u8fc7\u540e\u800560\u500d\u7684\u5185\u5b58\u3002\n\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u66f4\u5173\u5fc3\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u8bdd\uff0c\u90a3\u4e48\u589e\u91cf\u5f0f\u7684\u7248\u672c\u5b8c\u80dc\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.5 \u5c06\u5b57\u5178\u8f6c\u6362\u4e3aXML\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528\u4e00\u4e2aPython\u5b57\u5178\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u5c06\u5b83\u8f6c\u6362\u6210XML\u683c\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1 xml.etree.ElementTree \u5e93\u901a\u5e38\u7528\u6765\u505a\u89e3\u6790\u5de5\u4f5c\uff0c\u5176\u5b9e\u5b83\u4e5f\u53ef\u4ee5\u521b\u5efaXML\u6587\u6863\u3002\n\u4f8b\u5982\uff0c\u8003\u8651\u5982\u4e0b\u8fd9\u4e2a\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import Element\n\ndef dict_to_xml(tag, d):\n'''\nTurn a simple dict of key/value pairs into XML\n'''\nelem = Element(tag)\nfor key, val in d.items():\n    child = Element(key)\n    child.text = str(val)\n    elem.append(child)\nreturn elem"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = { 'name': 'GOOG', 'shares': 100, 'price':490.1 }\ne = dict_to_xml('stock', s)\ne"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8f6c\u6362\u7ed3\u679c\u662f\u4e00\u4e2a Element \u5b9e\u4f8b\u3002\u5bf9\u4e8eI/O\u64cd\u4f5c\uff0c\u4f7f\u7528 xml.etree.ElementTree \u4e2d\u7684 tostring()\n\u51fd\u6570\u5f88\u5bb9\u6613\u5c31\u80fd\u5c06\u5b83\u8f6c\u6362\u6210\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import tostring\ntostring(e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u7ed9\u67d0\u4e2a\u5143\u7d20\u6dfb\u52a0\u5c5e\u6027\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 set() \u65b9\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e.set('_id','1234')\ntostring(e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd8\u60f3\u4fdd\u6301\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u6784\u9020\u4e00\u4e2a OrderedDict \u6765\u4ee3\u66ff\u4e00\u4e2a\u666e\u901a\u7684\u5b57\u5178\u3002\u8bf7\u53c2\u80031.7\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u521b\u5efaXML\u7684\u65f6\u5019\uff0c\u4f60\u88ab\u9650\u5236\u53ea\u80fd\u6784\u9020\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u503c\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def dict_to_xml_str(tag, d):\n    '''\n    Turn a simple dict of key/value pairs into XML\n    '''\n    parts = ['<{}>'.format(tag)]\n    for key, val in d.items():\n        parts.append('<{0}>{1}</{0}>'.format(key,val))\n    parts.append('</{}>'.format(tag))\n    return ''.join(parts)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u95ee\u9898\u662f\u5982\u679c\u4f60\u624b\u52a8\u7684\u53bb\u6784\u9020\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u78b0\u5230\u4e00\u4e9b\u9ebb\u70e6\u3002\u4f8b\u5982\uff0c\u5f53\u5b57\u5178\u7684\u503c\u4e2d\u5305\u542b\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u7684\u65f6\u5019\u4f1a\u600e\u6837\u5462\uff1f"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = { 'name' : '<spam>' }"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# String creation\ndict_to_xml_str('item',d)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Proper XML creation\ne = dict_to_xml('item',d)\ntostring(e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u5230\u7a0b\u5e8f\u7684\u540e\u9762\u90a3\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26 \u2018<\u2019 \u548c \u2018>\u2019 \u88ab\u66ff\u6362\u6210\u4e86 &lt; \u548c &gt;"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u4ec5\u4f9b\u53c2\u8003\uff0c\u5982\u679c\u4f60\u9700\u8981\u624b\u52a8\u53bb\u8f6c\u6362\u8fd9\u4e9b\u5b57\u7b26\uff0c\n\u53ef\u4ee5\u4f7f\u7528 xml.sax.saxutils \u4e2d\u7684 escape()  \u548c unescape() \u51fd\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.sax.saxutils import escape, unescape\nescape('<spam>')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "unescape(_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9664\u4e86\u80fd\u521b\u5efa\u6b63\u786e\u7684\u8f93\u51fa\u5916\uff0c\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u539f\u56e0\u63a8\u8350\u4f60\u521b\u5efa Element \u5b9e\u4f8b\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\n\u90a3\u5c31\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7ec4\u5408\u6784\u9020\u4e00\u4e2a\u66f4\u5927\u7684\u6587\u6863\u5e76\u4e0d\u662f\u90a3\u4e48\u5bb9\u6613\u3002\n\u800c Element \u5b9e\u4f8b\u53ef\u4ee5\u4e0d\u7528\u8003\u8651\u89e3\u6790XML\u6587\u672c\u7684\u60c5\u51b5\u4e0b\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u88ab\u5904\u7406\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u4e0a\u5b8c\u6210\u4f60\u6240\u6709\u7684\u64cd\u4f5c\uff0c\u5e76\u5728\u6700\u540e\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5c06\u5176\u8f93\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.6 \u89e3\u6790\u548c\u4fee\u6539XML\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u53d6\u4e00\u4e2aXML\u6587\u6863\uff0c\u5bf9\u5b83\u6700\u4e00\u4e9b\u4fee\u6539\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u5199\u56deXML\u6587\u6863\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 xml.etree.ElementTree \u6a21\u5757\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002\n\u7b2c\u4e00\u6b65\u662f\u4ee5\u901a\u5e38\u7684\u65b9\u5f0f\u6765\u89e3\u6790\u8fd9\u4e2a\u6587\u6863\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a pred.xml \u7684\u6587\u6863\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "<?xml version=\"1.0\"?>\n<stop>\n    <id>14791</id>\n    <nm>Clark &amp; Balmoral</nm>\n    <sri>\n        <rt>22</rt>\n        <d>North Bound</d>\n        <dd>North Bound</dd>\n    </sri>\n    <cr>22</cr>\n    <pre>\n        <pt>5 MIN</pt>\n        <fd>Howard</fd>\n        <v>1378</v>\n        <rn>22</rn>\n    </pre>\n    <pre>\n        <pt>15 MIN</pt>\n        <fd>Howard</fd>\n        <v>1867</v>\n        <rn>22</rn>\n    </pre>\n</stop>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u5229\u7528 ElementTree \u6765\u8bfb\u53d6\u8fd9\u4e2a\u6587\u6863\u5e76\u5bf9\u5b83\u505a\u4e00\u4e9b\u4fee\u6539\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import parse, Element\ndoc = parse('pred.xml')\nroot = doc.getroot()\nroot"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Remove a few elements\nroot.remove(root.find('sri'))\nroot.remove(root.find('cr'))\n# Insert a new element after <nm>...</nm>\nroot.getchildren().index(root.find('nm'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e = Element('spam')\ne.text = 'This is a test'\nroot.insert(2, e)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Write back to a file\ndoc.write('newpred.xml', xml_declaration=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5904\u7406\u7ed3\u679c\u662f\u4e00\u4e2a\u50cf\u4e0b\u9762\u8fd9\u6837\u65b0\u7684XML\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "<?xml version='1.0' encoding='us-ascii'?>\n<stop>\n    <id>14791</id>\n    <nm>Clark &amp; Balmoral</nm>\n    <spam>This is a test</spam>\n    <pre>\n        <pt>5 MIN</pt>\n        <fd>Howard</fd>\n        <v>1378</v>\n        <rn>22</rn>\n    </pre>\n    <pre>\n        <pt>15 MIN</pt>\n        <fd>Howard</fd>\n        <v>1867</v>\n        <rn>22</rn>\n    </pre>\n</stop>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4fee\u6539\u4e00\u4e2aXML\u6587\u6863\u7ed3\u6784\u662f\u5f88\u5bb9\u6613\u7684\uff0c\u4f46\u662f\u4f60\u5fc5\u987b\u7262\u8bb0\u7684\u662f\u6240\u6709\u7684\u4fee\u6539\u90fd\u662f\u9488\u5bf9\u7236\u8282\u70b9\u5143\u7d20\uff0c\n\u5c06\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u5217\u8868\u6765\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5220\u9664\u67d0\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u8c03\u7528\u7236\u8282\u70b9\u7684 remove() \u65b9\u6cd5\u4ece\u5b83\u7684\u76f4\u63a5\u7236\u8282\u70b9\u4e2d\u5220\u9664\u3002\n\u5982\u679c\u4f60\u63d2\u5165\u6216\u589e\u52a0\u65b0\u7684\u5143\u7d20\uff0c\u4f60\u540c\u6837\u4f7f\u7528\u7236\u8282\u70b9\u5143\u7d20\u7684 insert() \u548c append() \u65b9\u6cd5\u3002\n\u8fd8\u80fd\u5bf9\u5143\u7d20\u4f7f\u7528\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\uff0c\u6bd4\u5982 element[i] \u6216 element[i:j]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u521b\u5efa\u65b0\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u672c\u8282\u65b9\u6848\u4e2d\u6f14\u793a\u7684 Element \u7c7b\u3002\u6211\u4eec\u57286.5\u5c0f\u8282\u5df2\u7ecf\u8be6\u7ec6\u8ba8\u8bba\u8fc7\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.7 \u5229\u7528\u547d\u540d\u7a7a\u95f4\u89e3\u6790XML\u6587\u6863\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u89e3\u6790\u67d0\u4e2aXML\u6587\u6863\uff0c\u6587\u6863\u4e2d\u4f7f\u7528\u4e86XML\u547d\u540d\u7a7a\u95f4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u4e86\u547d\u540d\u7a7a\u95f4\u7684\u6587\u6863\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<top>\n    <author>David Beazley</author>\n    <content>\n        <html xmlns=\"http://www.w3.org/1999/xhtml\">\n            <head>\n                <title>Hello World</title>\n            </head>\n            <body>\n                <h1>Hello World!</h1>\n            </body>\n        </html>\n    </content>\n</top>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u89e3\u6790\u8fd9\u4e2a\u6587\u6863\u5e76\u6267\u884c\u666e\u901a\u7684\u67e5\u8be2\uff0c\u4f60\u4f1a\u53d1\u73b0\u8fd9\u4e2a\u5e76\u4e0d\u662f\u90a3\u4e48\u5bb9\u6613\uff0c\u56e0\u4e3a\u6240\u6709\u6b65\u9aa4\u90fd\u53d8\u5f97\u76f8\u5f53\u7684\u7e41\u7410\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Some queries that work\ndoc.findtext('author')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "doc.find('content')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# A query involving a namespace (doesn't work)\ndoc.find('content/html')\n# Works if fully qualified\ndoc.find('content/{http://www.w3.org/1999/xhtml}html')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Doesn't work\ndoc.findtext('content/{http://www.w3.org/1999/xhtml}html/head/title')\n# Fully qualified\ndoc.findtext('content/{http://www.w3.org/1999/xhtml}html/'\n'{http://www.w3.org/1999/xhtml}head/{http://www.w3.org/1999/xhtml}title')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06\u547d\u540d\u7a7a\u95f4\u5904\u7406\u903b\u8f91\u5305\u88c5\u4e3a\u4e00\u4e2a\u5de5\u5177\u7c7b\u6765\u7b80\u5316\u8fd9\u4e2a\u8fc7\u7a0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class XMLNamespaces:\n    def __init__(self, **kwargs):\n        self.namespaces = {}\n        for name, uri in kwargs.items():\n            self.register(name, uri)\n    def register(self, name, uri):\n        self.namespaces[name] = '{'+uri+'}'\n    def __call__(self, path):\n        return path.format_map(self.namespaces)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ns = XMLNamespaces(html='http://www.w3.org/1999/xhtml')\ndoc.find(ns('content/{html}html'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "doc.findtext(ns('content/{html}html/{html}head/{html}title'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u89e3\u6790\u542b\u6709\u547d\u540d\u7a7a\u95f4\u7684XML\u6587\u6863\u4f1a\u6bd4\u8f83\u7e41\u7410\u3002\n\u4e0a\u9762\u7684 XMLNamespaces \u4ec5\u4ec5\u662f\u5141\u8bb8\u4f60\u4f7f\u7528\u7f29\u7565\u540d\u4ee3\u66ff\u5b8c\u6574\u7684URI\u5c06\u5176\u53d8\u5f97\u7a0d\u5fae\u7b80\u6d01\u4e00\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u4e0d\u5e78\u7684\u662f\uff0c\u5728\u57fa\u672c\u7684 ElementTree \u89e3\u6790\u4e2d\u6ca1\u6709\u4efb\u4f55\u9014\u5f84\u83b7\u53d6\u547d\u540d\u7a7a\u95f4\u7684\u4fe1\u606f\u3002\n\u4f46\u662f\uff0c\u5982\u679c\u4f60\u4f7f\u7528 iterparse() \u51fd\u6570\u7684\u8bdd\u5c31\u53ef\u4ee5\u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u547d\u540d\u7a7a\u95f4\u5904\u7406\u8303\u56f4\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from xml.etree.ElementTree import iterparse\nfor evt, elem in iterparse('ns2.xml', ('end', 'start-ns', 'end-ns')):\nprint(evt, elem)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "elem # This is the topmost element"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u70b9\uff0c\u5982\u679c\u4f60\u8981\u5904\u7406\u7684XML\u6587\u672c\u9664\u4e86\u8981\u4f7f\u7528\u5230\u5176\u4ed6\u9ad8\u7ea7XML\u7279\u6027\u5916\uff0c\u8fd8\u8981\u4f7f\u7528\u5230\u547d\u540d\u7a7a\u95f4\uff0c\n\u5efa\u8bae\u4f60\u6700\u597d\u662f\u4f7f\u7528 lxml \u51fd\u6570\u5e93\u6765\u4ee3\u66ff ElementTree \u3002\n\u4f8b\u5982\uff0clxml \u5bf9\u5229\u7528DTD\u9a8c\u8bc1\u6587\u6863\u3001\u66f4\u597d\u7684XPath\u652f\u6301\u548c\u4e00\u4e9b\u5176\u4ed6\u9ad8\u7ea7XML\u7279\u6027\u7b49\u90fd\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u652f\u6301\u3002\n\u8fd9\u4e00\u5c0f\u8282\u5176\u5b9e\u53ea\u662f\u6559\u4f60\u5982\u4f55\u8ba9XML\u89e3\u6790\u7a0d\u5fae\u7b80\u5355\u4e00\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.8 \u4e0e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u4ea4\u4e92\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u3001\u589e\u52a0\u6216\u5220\u9664\u8bb0\u5f55\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u4e2d\u8868\u793a\u591a\u884c\u6570\u636e\u7684\u6807\u51c6\u65b9\u5f0f\u662f\u4e00\u4e2a\u7531\u5143\u7ec4\u6784\u6210\u7684\u5e8f\u5217\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "stocks = [\n    ('GOOG', 100, 490.1),\n    ('AAPL', 50, 545.75),\n    ('FB', 150, 7.45),\n    ('HPQ', 75, 33.2),\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f9d\u636ePEP249\uff0c\u901a\u8fc7\u8fd9\u79cd\u5f62\u5f0f\u63d0\u4f9b\u6570\u636e\uff0c\n\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u4f7f\u7528Python\u6807\u51c6\u6570\u636e\u5e93API\u548c\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\n\u6240\u6709\u6570\u636e\u5e93\u4e0a\u7684\u64cd\u4f5c\u90fd\u901a\u8fc7SQL\u67e5\u8be2\u8bed\u53e5\u6765\u5b8c\u6210\u3002\u6bcf\u4e00\u884c\u8f93\u5165\u8f93\u51fa\u6570\u636e\u7528\u4e00\u4e2a\u5143\u7ec4\u6765\u8868\u793a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6f14\u793a\u8bf4\u660e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684 sqlite3 \u6a21\u5757\u3002\n\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u5e93(\u6bd4\u5982MySql\u3001Postgresql\u6216\u8005ODBC)\uff0c\n\u8fd8\u5f97\u5b89\u88c5\u76f8\u5e94\u7684\u7b2c\u4e09\u65b9\u6a21\u5757\u6765\u63d0\u4f9b\u652f\u6301\u3002\n\u4e0d\u8fc7\u76f8\u5e94\u7684\u7f16\u7a0b\u63a5\u53e3\u51e0\u4e4e\u90fd\u662f\u4e00\u6837\u7684\uff0c\u9664\u4e86\u4e00\u70b9\u70b9\u7ec6\u5fae\u5dee\u522b\u5916\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e00\u6b65\u662f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002\u901a\u5e38\u4f60\u8981\u6267\u884c connect() \u51fd\u6570\uff0c\n\u7ed9\u5b83\u63d0\u4f9b\u4e00\u4e9b\u6570\u636e\u5e93\u540d\u3001\u4e3b\u673a\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u5176\u4ed6\u5fc5\u8981\u7684\u4e00\u4e9b\u53c2\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sqlite3\ndb = sqlite3.connect('database.db')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5904\u7406\u6570\u636e\uff0c\u4e0b\u4e00\u6b65\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u3002\n\u4e00\u65e6\u4f60\u6709\u4e86\u6e38\u6807\uff0c\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u4e86\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = db.cursor()\nc.execute('create table portfolio (symbol text, shares integer, price real)')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "db.commit()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5411\u6570\u636e\u5e93\u8868\u4e2d\u63d2\u5165\u591a\u6761\u8bb0\u5f55\uff0c\u4f7f\u7528\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u8bed\u53e5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.executemany('insert into portfolio values (?,?,?)', stocks)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "db.commit()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6267\u884c\u67d0\u4e2a\u67e5\u8be2\uff0c\u4f7f\u7528\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u8bed\u53e5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for row in db.execute('select * from portfolio'):\n    print(row)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u63a5\u53d7\u7528\u6237\u8f93\u5165\u4f5c\u4e3a\u53c2\u6570\u6765\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u5fc5\u987b\u786e\u4fdd\u4f60\u4f7f\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u5360\u4f4d\u7b26``?``\u6765\u8fdb\u884c\u5f15\u7528\u53c2\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min_price = 100\nfor row in db.execute('select * from portfolio where price >= ?',"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "    print(row)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6bd4\u8f83\u4f4e\u7684\u7ea7\u522b\u4e0a\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\n\u4f60\u53ea\u9700\u63d0\u4f9bSQL\u8bed\u53e5\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u6a21\u5757\u5c31\u53ef\u4ee5\u66f4\u65b0\u6216\u63d0\u53d6\u6570\u636e\u4e86\u3002\n\u867d\u8bf4\u5982\u6b64\uff0c\u8fd8\u662f\u6709\u4e00\u4e9b\u6bd4\u8f83\u68d8\u624b\u7684\u7ec6\u8282\u95ee\u9898\u9700\u8981\u4f60\u9010\u4e2a\u5217\u51fa\u53bb\u89e3\u51b3\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u96be\u70b9\u662f\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u548cPython\u7c7b\u578b\u76f4\u63a5\u7684\u6620\u5c04\u3002\n\u5bf9\u4e8e\u65e5\u671f\u7c7b\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 datetime \u6a21\u5757\u4e2d\u7684 datetime \u5b9e\u4f8b\uff0c\n\u6216\u8005\u53ef\u80fd\u662f time \u6a21\u5757\u4e2d\u7684\u7cfb\u7edf\u65f6\u95f4\u6233\u3002\n\u5bf9\u4e8e\u6570\u5b57\u7c7b\u578b\uff0c\u7279\u522b\u662f\u4f7f\u7528\u5230\u5c0f\u6570\u7684\u91d1\u878d\u6570\u636e\uff0c\u53ef\u4ee5\u7528 decimal \u6a21\u5757\u4e2d\u7684 Decimal \u5b9e\u4f8b\u6765\u8868\u793a\u3002\n\u4e0d\u5e78\u7684\u662f\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u5e93\u800c\u8a00\u5177\u4f53\u6620\u5c04\u89c4\u5219\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u4f60\u5fc5\u987b\u53c2\u8003\u76f8\u5e94\u7684\u6587\u6863\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u4e00\u4e2a\u66f4\u52a0\u590d\u6742\u7684\u95ee\u9898\u5c31\u662fSQL\u8bed\u53e5\u5b57\u7b26\u4e32\u7684\u6784\u9020\u3002\n\u4f60\u5343\u4e07\u4e0d\u8981\u4f7f\u7528Python\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26(\u5982%)\u6216\u8005 .format() \u65b9\u6cd5\u6765\u521b\u5efa\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u3002\n\u5982\u679c\u4f20\u9012\u7ed9\u8fd9\u4e9b\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\u7684\u503c\u6765\u81ea\u4e8e\u7528\u6237\u7684\u8f93\u5165\uff0c\u90a3\u4e48\u4f60\u7684\u7a0b\u5e8f\u5c31\u5f88\u6709\u53ef\u80fd\u906d\u53d7SQL\u6ce8\u5165\u653b\u51fb(\u53c2\u8003 http://xkcd.com/327 )\u3002\n\u67e5\u8be2\u8bed\u53e5\u4e2d\u7684\u901a\u914d\u7b26 ? \u6307\u793a\u540e\u53f0\u6570\u636e\u5e93\u4f7f\u7528\u5b83\u81ea\u5df1\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u673a\u5236\uff0c\u8fd9\u6837\u66f4\u52a0\u7684\u5b89\u5168\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u5e78\u7684\u662f\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5e93\u540e\u53f0\u5bf9\u4e8e\u901a\u914d\u7b26\u7684\u4f7f\u7528\u662f\u4e0d\u4e00\u6837\u7684\u3002\u5927\u90e8\u5206\u6a21\u5757\u4f7f\u7528 ? \u6216 %s \uff0c\n\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u7b26\u53f7\uff0c\u6bd4\u5982:0\u6216:1\u6765\u6307\u793a\u53c2\u6570\u3002\n\u540c\u6837\u7684\uff0c\u4f60\u8fd8\u662f\u5f97\u53bb\u53c2\u8003\u4f60\u4f7f\u7528\u7684\u6570\u636e\u5e93\u6a21\u5757\u76f8\u5e94\u7684\u6587\u6863\u3002\n\u4e00\u4e2a\u6570\u636e\u5e93\u6a21\u5757\u7684 paramstyle \u5c5e\u6027\u5305\u542b\u4e86\u53c2\u6570\u5f15\u7528\u98ce\u683c\u7684\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5e93\u6570\u636e\u7684\u8bfb\u5199\u95ee\u9898\uff0c\u4f7f\u7528\u6570\u636e\u5e93API\u901a\u5e38\u975e\u5e38\u7b80\u5355\u3002\n\u5982\u679c\u4f60\u8981\u5904\u7406\u66f4\u52a0\u590d\u6742\u7684\u95ee\u9898\uff0c\u5efa\u8bae\u4f60\u4f7f\u7528\u66f4\u52a0\u9ad8\u7ea7\u7684\u63a5\u53e3\uff0c\u6bd4\u5982\u4e00\u4e2a\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04ORM\u6240\u63d0\u4f9b\u7684\u63a5\u53e3\u3002\n\u7c7b\u4f3c SQLAlchemy \u8fd9\u6837\u7684\u5e93\u5141\u8bb8\u4f60\u4f7f\u7528Python\u7c7b\u6765\u8868\u793a\u4e00\u4e2a\u6570\u636e\u5e93\u8868\uff0c\n\u5e76\u4e14\u80fd\u5728\u9690\u85cf\u5e95\u5c42SQL\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u5404\u79cd\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.9 \u7f16\u7801\u548c\u89e3\u7801\u5341\u516d\u8fdb\u5236\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u89e3\u7801\u6210\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u6216\u8005\u5c06\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u7f16\u7801\u6210\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53ea\u662f\u7b80\u5355\u7684\u89e3\u7801\u6216\u7f16\u7801\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u539f\u59cb\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u3000binascii \u6a21\u5757\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Initial byte string\ns = b'hello'\n# Encode as hex\nimport binascii\nh = binascii.b2a_hex(s)\nh"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Decode back to bytes\nbinascii.a2b_hex(h)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u4f3c\u7684\u529f\u80fd\u540c\u6837\u53ef\u4ee5\u5728 base64 \u6a21\u5757\u4e2d\u627e\u5230\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import base64\nh = base64.b16encode(s)\nh"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "base64.b16decode(h)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u7684\u51fd\u6570\u6765\u8f6c\u6362\u5341\u516d\u8fdb\u5236\u662f\u5f88\u7b80\u5355\u7684\u3002\n\u4e0a\u9762\u4e24\u79cd\u6280\u672f\u7684\u4e3b\u8981\u4e0d\u540c\u5728\u4e8e\u5927\u5c0f\u5199\u7684\u5904\u7406\u3002\n\u51fd\u6570 base64.b16decode() \u548c base64.b16encode() \u53ea\u80fd\u64cd\u4f5c\u5927\u5199\u5f62\u5f0f\u7684\u5341\u516d\u8fdb\u5236\u5b57\u6bcd\uff0c\n\u800c binascii \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u5927\u5c0f\u5199\u90fd\u80fd\u5904\u7406\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\u7f16\u7801\u51fd\u6570\u6240\u4ea7\u751f\u7684\u8f93\u51fa\u603b\u662f\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u3002\n\u5982\u679c\u60f3\u5f3a\u5236\u4ee5Unicode\u5f62\u5f0f\u8f93\u51fa\uff0c\u4f60\u9700\u8981\u589e\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u754c\u9762\u6b65\u9aa4\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "h = base64.b16encode(s)\nprint(h)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(h.decode('ascii'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u89e3\u7801\u5341\u516d\u8fdb\u5236\u6570\u65f6\uff0c\u51fd\u6570 b16decode() \u548c a2b_hex() \u53ef\u4ee5\u63a5\u53d7\u5b57\u8282\u6216unicode\u5b57\u7b26\u4e32\u3002\n\u4f46\u662f\uff0cunicode\u5b57\u7b26\u4e32\u5fc5\u987b\u4ec5\u4ec5\u53ea\u5305\u542bASCII\u7f16\u7801\u7684\u5341\u516d\u8fdb\u5236\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.10 \u7f16\u7801\u89e3\u7801Base64\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u4f7f\u7528Base64\u683c\u5f0f\u89e3\u7801\u6216\u7f16\u7801\u4e8c\u8fdb\u5236\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "base64 \u6a21\u5757\u4e2d\u6709\u4e24\u4e2a\u51fd\u6570 b64encode() and b64decode() \u53ef\u4ee5\u5e2e\u4f60\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f8b\u5982;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Some byte data\ns = b'hello'\nimport base64"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Encode as Base64\na = base64.b64encode(s)\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Decode from Base64\nbase64.b64decode(a)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Base64\u7f16\u7801\u4ec5\u4ec5\u7528\u4e8e\u9762\u5411\u5b57\u8282\u7684\u6570\u636e\u6bd4\u5982\u5b57\u8282\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u3002\n\u6b64\u5916\uff0c\u7f16\u7801\u5904\u7406\u7684\u8f93\u51fa\u7ed3\u679c\u603b\u662f\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u3002\n\u5982\u679c\u4f60\u60f3\u6df7\u5408\u4f7f\u7528Base64\u7f16\u7801\u7684\u6570\u636e\u548cUnicode\u6587\u672c\uff0c\u4f60\u5fc5\u987b\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u89e3\u7801\u6b65\u9aa4\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = base64.b64encode(s).decode('ascii')\na"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u89e3\u7801Base64\u7684\u65f6\u5019\uff0c\u5b57\u8282\u5b57\u7b26\u4e32\u548cUnicode\u6587\u672c\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u3002\n\u4f46\u662f\uff0cUnicode\u5b57\u7b26\u4e32\u53ea\u80fd\u5305\u542bASCII\u5b57\u7b26\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.11 \u8bfb\u5199\u4e8c\u8fdb\u5236\u6570\u7ec4\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u7ec4\u7684\u7ed3\u6784\u5316\u6570\u636e\u5230Python\u5143\u7ec4\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u4f7f\u7528 struct \u6a21\u5757\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002\n\u4e0b\u9762\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\u5c06\u4e00\u4e2aPython\u5143\u7ec4\u5217\u8868\u5199\u5165\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 struct \u5c06\u6bcf\u4e2a\u5143\u7ec4\u7f16\u7801\u4e3a\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\ndef write_records(records, format, f):\n    '''\n    Write a sequence of tuples to a binary file of structures.\n    '''\n    record_struct = Struct(format)\n    for r in records:\n        f.write(record_struct.pack(*r))\n\n# Example\nif __name__ == '__main__':\n    records = [ (1, 2.3, 4.5),\n                (6, 7.8, 9.0),\n                (12, 13.4, 56.7) ]\n    with open('data.b', 'wb') as f:\n        write_records(records, '<idd', f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\u3002\n\u9996\u5148\uff0c\u5982\u679c\u4f60\u6253\u7b97\u4ee5\u5757\u7684\u5f62\u5f0f\u589e\u91cf\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\n\ndef read_records(format, f):\n    record_struct = Struct(format)\n    chunks = iter(lambda: f.read(record_struct.size), b'')\n    return (record_struct.unpack(chunk) for chunk in chunks)\n\n# Example\nif __name__ == '__main__':\n    with open('data.b','rb') as f:\n        for rec in read_records('<idd', f):\n            # Process rec\n            ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5c06\u6574\u4e2a\u6587\u4ef6\u4e00\u6b21\u6027\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u4e2d\uff0c\u7136\u540e\u5728\u5206\u7247\u89e3\u6790\u3002\u90a3\u4e48\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\n\ndef unpack_records(format, data):\n    record_struct = Struct(format)\n    return (record_struct.unpack_from(data, offset)\n            for offset in range(0, len(data), record_struct.size))\n\n# Example\nif __name__ == '__main__':\n    with open('data.b', 'rb') as f:\n        data = f.read()\n    for rec in unpack_records('<idd', data):\n        # Process rec\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e24\u79cd\u60c5\u51b5\u4e0b\u7684\u7ed3\u679c\u90fd\u662f\u4e00\u4e2a\u53ef\u8fd4\u56de\u7528\u6765\u521b\u5efa\u8be5\u6587\u4ef6\u7684\u539f\u59cb\u5143\u7ec4\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u9700\u8981\u7f16\u7801\u548c\u89e3\u7801\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7a0b\u5e8f\u800c\u8a00\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 struct \u6a21\u5757\u3002\n\u4e3a\u4e86\u58f0\u660e\u4e00\u4e2a\u65b0\u7684\u7ed3\u6784\u4f53\uff0c\u53ea\u9700\u8981\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a Struct \u5b9e\u4f8b\u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Little endian 32-bit integer, two double precision floats\nrecord_struct = Struct('<idd')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u6784\u4f53\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u4e9b\u7ed3\u6784\u7801\u503ci, d, f\u7b49\n[\u53c2\u8003 Python\u6587\u6863 ]\u3002\n\u8fd9\u4e9b\u4ee3\u7801\u5206\u522b\u4ee3\u8868\u67d0\u4e2a\u7279\u5b9a\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u7c7b\u578b\u598232\u4f4d\u6574\u6570\uff0c64\u4f4d\u6d6e\u70b9\u6570\uff0c32\u4f4d\u6d6e\u70b9\u6570\u7b49\u3002\n\u7b2c\u4e00\u4e2a\u5b57\u7b26 < \u6307\u5b9a\u4e86\u5b57\u8282\u987a\u5e8f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b83\u8868\u793a\u201d\u4f4e\u4f4d\u5728\u524d\u201d\u3002\n\u66f4\u6539\u8fd9\u4e2a\u5b57\u7b26\u4e3a > \u8868\u793a\u9ad8\u4f4d\u5728\u524d\uff0c\u6216\u8005\u662f ! \u8868\u793a\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ea7\u751f\u7684 Struct \u5b9e\u4f8b\u6709\u5f88\u591a\u5c5e\u6027\u548c\u65b9\u6cd5\u7528\u6765\u64cd\u4f5c\u76f8\u5e94\u7c7b\u578b\u7684\u7ed3\u6784\u3002\nsize \u5c5e\u6027\u5305\u542b\u4e86\u7ed3\u6784\u7684\u5b57\u8282\u6570\uff0c\u8fd9\u5728I/O\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002\npack() \u548c unpack() \u65b9\u6cd5\u88ab\u7528\u6765\u6253\u5305\u548c\u89e3\u5305\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from struct import Struct\nrecord_struct = Struct('<idd')\nrecord_struct.size"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "record_struct.pack(1, 2.0, 3.0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "record_struct.unpack(_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u65f6\u5019\u4f60\u8fd8\u4f1a\u770b\u5230 pack() \u548c unpack() \u64cd\u4f5c\u4ee5\u6a21\u5757\u7ea7\u522b\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\nstruct.pack('<idd', 1, 2.0, 3.0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "struct.unpack('<idd', _)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u611f\u89c9\u6ca1\u6709\u5b9e\u4f8b\u65b9\u6cd5\u90a3\u4e48\u4f18\u96c5\uff0c\u7279\u522b\u662f\u5728\u4f60\u4ee3\u7801\u4e2d\u540c\u6837\u7684\u7ed3\u6784\u51fa\u73b0\u5728\u591a\u4e2a\u5730\u65b9\u7684\u65f6\u5019\u3002\n\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a Struct \u5b9e\u4f8b\uff0c\u683c\u5f0f\u4ee3\u7801\u53ea\u4f1a\u6307\u5b9a\u4e00\u6b21\u5e76\u4e14\u6240\u6709\u7684\u64cd\u4f5c\u88ab\u96c6\u4e2d\u5904\u7406\u3002\n\u8fd9\u6837\u4e00\u6765\u4ee3\u7801\u7ef4\u62a4\u5c31\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u4e86(\u56e0\u4e3a\u4f60\u53ea\u9700\u8981\u6539\u53d8\u4e00\u5904\u4ee3\u7801\u5373\u53ef)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bfb\u53d6\u4e8c\u8fdb\u5236\u7ed3\u6784\u7684\u4ee3\u7801\u8981\u7528\u5230\u4e00\u4e9b\u975e\u5e38\u6709\u8da3\u800c\u4f18\u7f8e\u7684\u7f16\u7a0b\u6280\u5de7\u3002\n\u5728\u51fd\u6570\u3000read_records \u4e2d\uff0citer() \u88ab\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u56fa\u5b9a\u5927\u5c0f\u6570\u636e\u5757\u7684\u8fed\u4ee3\u5668\uff0c\u53c2\u80035.8\u5c0f\u8282\u3002\n\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4f1a\u4e0d\u65ad\u7684\u8c03\u7528\u4e00\u4e2a\u7528\u6237\u63d0\u4f9b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61(\u6bd4\u5982 lambda: f.read(record_struct.size) )\uff0c\n\u76f4\u5230\u5b83\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u503c(\u5982b\u2019\u2018)\uff0c\u8fd9\u65f6\u5019\u8fed\u4ee3\u505c\u6b62\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('data.b', 'rb')\nchunks = iter(lambda: f.read(20), b'')\nchunks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for chk in chunks:\nprint(chk)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u4f60\u6240\u89c1\uff0c\u521b\u5efa\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u4e00\u4e2a\u539f\u56e0\u662f\u5b83\u80fd\u5141\u8bb8\u4f7f\u7528\u4e00\u4e2a\u751f\u6210\u5668\u63a8\u5bfc\u6765\u521b\u5efa\u8bb0\u5f55\u3002\n\u5982\u679c\u4f60\u4e0d\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\uff0c\u90a3\u4e48\u4ee3\u7801\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def read_records(format, f):\n    record_struct = Struct(format)\n    while True:\n        chk = f.read(record_struct.size)\n        if chk == b'':\n            break\n        yield record_struct.unpack(chk)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u51fd\u6570 unpack_records() \u4e2d\u4f7f\u7528\u4e86\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5 unpack_from() \u3002\nunpack_from() \u5bf9\u4e8e\u4ece\u4e00\u4e2a\u5927\u578b\u4e8c\u8fdb\u5236\u6570\u7ec4\u4e2d\u63d0\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u975e\u5e38\u6709\u7528\uff0c\n\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u7684\u4e34\u65f6\u5bf9\u8c61\u6216\u8005\u8fdb\u884c\u5185\u5b58\u590d\u5236\u64cd\u4f5c\u3002\n\u4f60\u53ea\u9700\u8981\u7ed9\u5b83\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32(\u6216\u6570\u7ec4)\u548c\u4e00\u4e2a\u5b57\u8282\u504f\u79fb\u91cf\uff0c\u5b83\u4f1a\u4ece\u90a3\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u76f4\u63a5\u89e3\u5305\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4f7f\u7528 unpack() \u6765\u4ee3\u66ff unpack_from() \uff0c\n\u4f60\u9700\u8981\u4fee\u6539\u4ee3\u7801\u6765\u6784\u9020\u5927\u91cf\u7684\u5c0f\u7684\u5207\u7247\u4ee5\u53ca\u8fdb\u884c\u504f\u79fb\u91cf\u7684\u8ba1\u7b97\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def unpack_records(format, data):\n    record_struct = Struct(format)\n    return (record_struct.unpack(data[offset:offset + record_struct.size])\n            for offset in range(0, len(data), record_struct.size))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u9664\u4e86\u4ee3\u7801\u770b\u4e0a\u53bb\u5f88\u590d\u6742\u5916\uff0c\u8fd8\u5f97\u505a\u5f88\u591a\u989d\u5916\u7684\u5de5\u4f5c\uff0c\u56e0\u4e3a\u5b83\u6267\u884c\u4e86\u5927\u91cf\u7684\u504f\u79fb\u91cf\u8ba1\u7b97\uff0c\n\u590d\u5236\u6570\u636e\u4ee5\u53ca\u6784\u9020\u5c0f\u7684\u5207\u7247\u5bf9\u8c61\u3002\n\u5982\u679c\u4f60\u51c6\u5907\u4ece\u8bfb\u53d6\u5230\u7684\u4e00\u4e2a\u5927\u578b\u5b57\u8282\u5b57\u7b26\u4e32\u4e2d\u89e3\u5305\u5927\u91cf\u7684\u7ed3\u6784\u4f53\u7684\u8bdd\uff0cunpack_from() \u4f1a\u8868\u73b0\u7684\u66f4\u51fa\u8272\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u89e3\u5305\u7684\u65f6\u5019\uff0ccollections \u6a21\u5757\u4e2d\u7684\u547d\u540d\u5143\u7ec4\u5bf9\u8c61\u6216\u8bb8\u662f\u4f60\u60f3\u8981\u7528\u5230\u7684\u3002\n\u5b83\u53ef\u4ee5\u8ba9\u4f60\u7ed9\u8fd4\u56de\u5143\u7ec4\u8bbe\u7f6e\u5c5e\u6027\u540d\u79f0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import namedtuple\n\nRecord = namedtuple('Record', ['kind','x','y'])\n\nwith open('data.p', 'rb') as f:\n    records = (Record(*r) for r in read_records('<idd', f))\n\nfor r in records:\n    print(r.kind, r.x, r.y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f60\u6700\u597d\u4f7f\u7528 numpy \u6a21\u5757\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u636e\u8bfb\u53d6\u5230\u4e00\u4e2a\u7ed3\u6784\u5316\u6570\u7ec4\u4e2d\u800c\u4e0d\u662f\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\u4e2d\u3002\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\nf = open('data.b', 'rb')\nrecords = np.fromfile(f, dtype='<i,<d,<d')\nrecords"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "records[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "records[1]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u63d0\u4e00\u70b9\uff0c\u5982\u679c\u4f60\u9700\u8981\u4ece\u5df2\u77e5\u7684\u6587\u4ef6\u683c\u5f0f(\u5982\u56fe\u7247\u683c\u5f0f\uff0c\u56fe\u5f62\u6587\u4ef6\uff0cHDF5\u7b49)\u4e2d\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\n\u5148\u68c0\u67e5\u770b\u770bPython\u662f\u4e0d\u662f\u5df2\u7ecf\u63d0\u4f9b\u4e86\u73b0\u5b58\u7684\u6a21\u5757\u3002\u56e0\u4e3a\u4e0d\u5230\u4e07\u4e0d\u5f97\u5df2\u6ca1\u6709\u5fc5\u8981\u53bb\u91cd\u590d\u9020\u8f6e\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.12 \u8bfb\u53d6\u5d4c\u5957\u548c\u53ef\u53d8\u957f\u4e8c\u8fdb\u5236\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u8bfb\u53d6\u5305\u542b\u5d4c\u5957\u6216\u8005\u53ef\u53d8\u957f\u8bb0\u5f55\u96c6\u5408\u7684\u590d\u6742\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6570\u636e\u3002\u8fd9\u4e9b\u6570\u636e\u53ef\u80fd\u5305\u542b\u56fe\u7247\u3001\u89c6\u9891\u3001\u7535\u5b50\u5730\u56fe\u6587\u4ef6\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "struct \u6a21\u5757\u53ef\u88ab\u7528\u6765\u7f16\u7801/\u89e3\u7801\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u4e8c\u8fdb\u5236\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e3a\u4e86\u89e3\u91ca\u6e05\u695a\u8fd9\u79cd\u6570\u636e\uff0c\u5047\u8bbe\u4f60\u7528\u4e0b\u9762\u7684Python\u6570\u636e\u7ed3\u6784\n\u6765\u8868\u793a\u4e00\u4e2a\u7ec4\u6210\u4e00\u7cfb\u5217\u591a\u8fb9\u5f62\u7684\u70b9\u7684\u96c6\u5408\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "polys = [\n    [ (1.0, 2.5), (3.5, 4.0), (2.5, 1.5) ],\n    [ (7.0, 1.2), (5.1, 3.0), (0.5, 7.5), (0.8, 9.0) ],\n    [ (3.4, 6.3), (1.2, 0.5), (4.6, 9.2) ],\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u5047\u8bbe\u8fd9\u4e2a\u6570\u636e\u88ab\u7f16\u7801\u5230\u4e00\u4e2a\u4ee5\u4e0b\u5217\u5934\u90e8\u5f00\u59cb\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u53bb\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "+------+--------+------------------------------------+\n|Byte  | Type   |  Description                       |\n+======+========+====================================+\n|0     | int    |  \u6587\u4ef6\u4ee3\u7801\uff080x1234\uff0c\u5c0f\u7aef\uff09          |\n+------+--------+------------------------------------+\n|4     | double |  x \u7684\u6700\u5c0f\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|12    | double |  y \u7684\u6700\u5c0f\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|20    | double |  x \u7684\u6700\u5927\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|28    | double |  y \u7684\u6700\u5927\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|36    | int    |  \u4e09\u89d2\u5f62\u6570\u91cf\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7d27\u8ddf\u7740\u5934\u90e8\u662f\u4e00\u7cfb\u5217\u7684\u591a\u8fb9\u5f62\u8bb0\u5f55\uff0c\u7f16\u7801\u683c\u5f0f\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "+------+--------+-------------------------------------------+\n|Byte  | Type   |  Description                              |\n+======+========+===========================================+\n|0     | int    |  \u8bb0\u5f55\u957f\u5ea6\uff08N\u5b57\u8282\uff09                        |\n+------+--------+-------------------------------------------+\n|4-N   | Points |  (X,Y) \u5750\u6807\uff0c\u4ee5\u6d6e\u70b9\u6570\u8868\u793a                 |\n+------+--------+-------------------------------------------+"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5199\u8fd9\u6837\u7684\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684Python\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\nimport itertools\n\ndef write_polys(filename, polys):\n    # Determine bounding box\n    flattened = list(itertools.chain(*polys))\n    min_x = min(x for x, y in flattened)\n    max_x = max(x for x, y in flattened)\n    min_y = min(y for x, y in flattened)\n    max_y = max(y for x, y in flattened)\n    with open(filename, 'wb') as f:\n        f.write(struct.pack('<iddddi', 0x1234,\n                            min_x, min_y,\n                            max_x, max_y,\n                            len(polys)))\n        for poly in polys:\n            size = len(poly) * struct.calcsize('<dd')\n            f.write(struct.pack('<i', size + 4))\n            for pt in poly:\n                f.write(struct.pack('<dd', *pt))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u6570\u636e\u8bfb\u53d6\u56de\u6765\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5229\u7528\u51fd\u6570 struct.unpack() \uff0c\u4ee3\u7801\u5f88\u76f8\u4f3c\uff0c\u57fa\u672c\u5c31\u662f\u4e0a\u9762\u5199\u64cd\u4f5c\u7684\u9006\u5e8f\u3002\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def read_polys(filename):\n    with open(filename, 'rb') as f:\n        # Read the header\n        header = f.read(40)\n        file_code, min_x, min_y, max_x, max_y, num_polys = \\\n            struct.unpack('<iddddi', header)\n        polys = []\n        for n in range(num_polys):\n            pbytes, = struct.unpack('<i', f.read(4))\n            poly = []\n            for m in range(pbytes // 16):\n                pt = struct.unpack('<dd', f.read(16))\n                poly.append(pt)\n            polys.append(poly)\n    return polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e2a\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u91cc\u9762\u6df7\u6742\u4e86\u5f88\u591a\u8bfb\u53d6\u3001\u89e3\u5305\u6570\u636e\u7ed3\u6784\u548c\u5176\u4ed6\u7ec6\u8282\u7684\u4ee3\u7801\u3002\u5982\u679c\u7528\u8fd9\u6837\u7684\u4ee3\u7801\u6765\u5904\u7406\u771f\u5b9e\u7684\u6570\u636e\u6587\u4ef6\uff0c\n\u90a3\u672a\u514d\u4e5f\u592a\u7e41\u6742\u4e86\u70b9\u3002\u56e0\u6b64\u5f88\u663e\u7136\u5e94\u8be5\u6709\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u8ba9\u7a0b\u5e8f\u5458\u53ea\u5173\u6ce8\u81ea\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u672c\u5c0f\u8282\u63a5\u4e0b\u6765\u7684\u90e8\u5206\uff0c\u6211\u4f1a\u9010\u6b65\u6f14\u793a\u4e00\u4e2a\u66f4\u52a0\u4f18\u79c0\u7684\u89e3\u6790\u5b57\u8282\u6570\u636e\u7684\u65b9\u6848\u3002\n\u76ee\u6807\u662f\u53ef\u4ee5\u7ed9\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u7ea7\u7684\u6587\u4ef6\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5e76\u7b80\u5316\u8bfb\u53d6\u548c\u89e3\u5305\u6570\u636e\u7684\u7ec6\u8282\u3002\u4f46\u662f\u6211\u8981\u5148\u63d0\u9192\u4f60\uff0c\n\u672c\u5c0f\u8282\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4ee3\u7801\u5e94\u8be5\u662f\u6574\u672c\u4e66\u4e2d\u6700\u590d\u6742\u6700\u9ad8\u7ea7\u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u5143\u7f16\u7a0b\u6280\u672f\u3002\n\u4e00\u5b9a\u8981\u4ed4\u7ec6\u7684\u9605\u8bfb\u6211\u4eec\u7684\u8ba8\u8bba\u90e8\u5206\uff0c\u53e6\u5916\u4e5f\u8981\u53c2\u8003\u4e0b\u5176\u4ed6\u7ae0\u8282\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u5f53\u8bfb\u53d6\u5b57\u8282\u6570\u636e\u7684\u65f6\u5019\uff0c\u901a\u5e38\u5728\u6587\u4ef6\u5f00\u59cb\u90e8\u5206\u4f1a\u5305\u542b\u6587\u4ef6\u5934\u548c\u5176\u4ed6\u7684\u6570\u636e\u7ed3\u6784\u3002\n\u5c3d\u7ba1struct\u6a21\u5757\u53ef\u4ee5\u89e3\u5305\u8fd9\u4e9b\u6570\u636e\u5230\u4e00\u4e2a\u5143\u7ec4\u4e2d\u53bb\uff0c\u53e6\u5916\u4e00\u79cd\u8868\u793a\u8fd9\u79cd\u4fe1\u606f\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528\u4e00\u4e2a\u7c7b\u3002\n\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\n\nclass StructField:\n    '''\n    Descriptor representing a simple structure field\n    '''\n    def __init__(self, format, offset):\n        self.format = format\n        self.offset = offset\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            r = struct.unpack_from(self.format, instance._buffer, self.offset)\n            return r[0] if len(r) == 1 else r\n\nclass Structure:\n    def __init__(self, bytedata):\n        self._buffer = memoryview(bytedata)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u63cf\u8ff0\u5668\u6765\u8868\u793a\u6bcf\u4e2a\u7ed3\u6784\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u63cf\u8ff0\u5668\u5305\u542b\u4e00\u4e2a\u7ed3\u6784\u517c\u5bb9\u683c\u5f0f\u7684\u4ee3\u7801\u4ee5\u53ca\u4e00\u4e2a\u5b57\u8282\u504f\u79fb\u91cf\uff0c\n\u5b58\u50a8\u5728\u5185\u90e8\u7684\u5185\u5b58\u7f13\u51b2\u4e2d\u3002\u5728 __get__() \u65b9\u6cd5\u4e2d\uff0cstruct.unpack_from()\n\u51fd\u6570\u88ab\u7528\u6765\u4ece\u7f13\u51b2\u4e2d\u89e3\u5305\u4e00\u4e2a\u503c\uff0c\u7701\u53bb\u4e86\u989d\u5916\u7684\u5206\u7247\u6216\u590d\u5236\u64cd\u4f5c\u6b65\u9aa4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Structure \u7c7b\u5c31\u662f\u4e00\u4e2a\u57fa\u7840\u7c7b\uff0c\u63a5\u53d7\u5b57\u8282\u6570\u636e\u5e76\u5b58\u50a8\u5728\u5185\u90e8\u7684\u5185\u5b58\u7f13\u51b2\u4e2d\uff0c\u5e76\u88ab StructField \u63cf\u8ff0\u5668\u4f7f\u7528\u3002\n\u8fd9\u91cc\u4f7f\u7528\u4e86 memoryview() \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8be6\u7ec6\u8bb2\u89e3\u5b83\u662f\u7528\u6765\u5e72\u561b\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u4ee3\u7801\uff0c\u4f60\u73b0\u5728\u5c31\u80fd\u5b9a\u4e49\u4e00\u4e2a\u9ad8\u5c42\u6b21\u7684\u7ed3\u6784\u5bf9\u8c61\u6765\u8868\u793a\u4e0a\u9762\u8868\u683c\u4fe1\u606f\u6240\u671f\u671b\u7684\u6587\u4ef6\u683c\u5f0f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class PolyHeader(Structure):\n    file_code = StructField('<i', 0)\n    min_x = StructField('<d', 4)\n    min_y = StructField('<d', 12)\n    max_x = StructField('<d', 20)\n    max_y = StructField('<d', 28)\n    num_polys = StructField('<i', 36)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u4f8b\u5b50\u5229\u7528\u8fd9\u4e2a\u7c7b\u6765\u8bfb\u53d6\u4e4b\u524d\u6211\u4eec\u5199\u5165\u7684\u591a\u8fb9\u5f62\u6570\u636e\u7684\u5934\u90e8\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader(f.read(40))\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u5f88\u6709\u8da3\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8fd8\u662f\u6709\u4e00\u4e9b\u70e6\u4eba\u7684\u5730\u65b9\u3002\u9996\u5148\uff0c\u5c3d\u7ba1\u4f60\u83b7\u5f97\u4e86\u4e00\u4e2a\u7c7b\u63a5\u53e3\u7684\u4fbf\u5229\uff0c\n\u4f46\u662f\u8fd9\u4e2a\u4ee3\u7801\u8fd8\u662f\u6709\u70b9\u81c3\u80bf\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u6307\u5b9a\u5f88\u591a\u5e95\u5c42\u7684\u7ec6\u8282(\u6bd4\u5982\u91cd\u590d\u4f7f\u7528 StructField \uff0c\u6307\u5b9a\u504f\u79fb\u91cf\u7b49)\u3002\n\u53e6\u5916\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u7c7b\u540c\u6837\u786e\u5b9e\u4e00\u4e9b\u4fbf\u5229\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u7ed3\u6784\u7684\u603b\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u4f60\u9047\u5230\u4e86\u50cf\u8fd9\u6837\u5197\u4f59\u7684\u7c7b\u5b9a\u4e49\uff0c\u4f60\u5e94\u8be5\u8003\u8651\u4e0b\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\u6216\u5143\u7c7b\u3002\n\u5143\u7c7b\u6709\u4e00\u4e2a\u7279\u6027\u5c31\u662f\u5b83\u80fd\u591f\u88ab\u7528\u6765\u586b\u5145\u8bb8\u591a\u4f4e\u5c42\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u4ece\u800c\u91ca\u653e\u4f7f\u7528\u8005\u7684\u8d1f\u62c5\u3002\n\u4e0b\u9762\u6211\u6765\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f7f\u7528\u5143\u7c7b\u7a0d\u5fae\u6539\u9020\u4e0b\u6211\u4eec\u7684 Structure \u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class StructureMeta(type):\n    '''\n    Metaclass that automatically creates StructField descriptors\n    '''\n    def __init__(self, clsname, bases, clsdict):\n        fields = getattr(self, '_fields_', [])\n        byte_order = ''\n        offset = 0\n        for format, fieldname in fields:\n            if format.startswith(('<','>','!','@')):\n                byte_order = format[0]\n                format = format[1:]\n            format = byte_order + format\n            setattr(self, fieldname, StructField(format, offset))\n            offset += struct.calcsize(format)\n        setattr(self, 'struct_size', offset)\n\nclass Structure(metaclass=StructureMeta):\n    def __init__(self, bytedata):\n        self._buffer = bytedata\n\n    @classmethod\n    def from_file(cls, f):\n        return cls(f.read(cls.struct_size))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u65b0\u7684 Structure \u7c7b\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        ('d', 'min_x'),\n        ('d', 'min_y'),\n        ('d', 'max_x'),\n        ('d', 'max_y'),\n        ('i', 'num_polys')\n    ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u8fd9\u6837\u5199\u5c31\u7b80\u5355\u591a\u4e86\u3002\u6211\u4eec\u6dfb\u52a0\u7684\u7c7b\u65b9\u6cd5 from_file()\n\u8ba9\u6211\u4eec\u5728\u4e0d\u9700\u8981\u77e5\u9053\u4efb\u4f55\u6570\u636e\u7684\u5927\u5c0f\u548c\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u5c31\u80fd\u8f7b\u677e\u7684\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u5f00\u59cb\u4f7f\u7528\u4e86\u5143\u7c7b\uff0c\u4f60\u5c31\u53ef\u4ee5\u8ba9\u5b83\u53d8\u5f97\u66f4\u52a0\u667a\u80fd\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u8fd8\u60f3\u652f\u6301\u5d4c\u5957\u7684\u5b57\u8282\u7ed3\u6784\uff0c\n\u4e0b\u9762\u662f\u5bf9\u524d\u9762\u5143\u7c7b\u7684\u4e00\u4e2a\u5c0f\u7684\u6539\u8fdb\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u8f85\u52a9\u63cf\u8ff0\u5668\u6765\u8fbe\u5230\u60f3\u8981\u7684\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class NestedStruct:\n    '''\n    Descriptor representing a nested structure\n    '''\n    def __init__(self, name, struct_type, offset):\n        self.name = name\n        self.struct_type = struct_type\n        self.offset = offset\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            data = instance._buffer[self.offset:\n                            self.offset+self.struct_type.struct_size]\n            result = self.struct_type(data)\n            # Save resulting structure back on instance to avoid\n            # further recomputation of this step\n            setattr(instance, self.name, result)\n            return result\n\nclass StructureMeta(type):\n    '''\n    Metaclass that automatically creates StructField descriptors\n    '''\n    def __init__(self, clsname, bases, clsdict):\n        fields = getattr(self, '_fields_', [])\n        byte_order = ''\n        offset = 0\n        for format, fieldname in fields:\n            if isinstance(format, StructureMeta):\n                setattr(self, fieldname,\n                        NestedStruct(fieldname, format, offset))\n                offset += format.struct_size\n            else:\n                if format.startswith(('<','>','!','@')):\n                    byte_order = format[0]\n                    format = format[1:]\n                format = byte_order + format\n                setattr(self, fieldname, StructField(format, offset))\n                offset += struct.calcsize(format)\n        setattr(self, 'struct_size', offset)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cNestedStruct \u63cf\u8ff0\u5668\u88ab\u7528\u6765\u53e0\u52a0\u53e6\u5916\u4e00\u4e2a\u5b9a\u4e49\u5728\u67d0\u4e2a\u5185\u5b58\u533a\u57df\u4e0a\u7684\u7ed3\u6784\u3002\n\u5b83\u901a\u8fc7\u5c06\u539f\u59cb\u5185\u5b58\u7f13\u51b2\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u540e\u5b9e\u4f8b\u5316\u7ed9\u5b9a\u7684\u7ed3\u6784\u7c7b\u578b\u3002\u7531\u4e8e\u5e95\u5c42\u7684\u5185\u5b58\u7f13\u51b2\u533a\u662f\u901a\u8fc7\u4e00\u4e2a\u5185\u5b58\u89c6\u56fe\u521d\u59cb\u5316\u7684\uff0c\n\u6240\u4ee5\u8fd9\u79cd\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u5f15\u53d1\u4efb\u4f55\u7684\u989d\u5916\u7684\u5185\u5b58\u590d\u5236\u3002\u76f8\u53cd\uff0c\u5b83\u4ec5\u4ec5\u5c31\u662f\u4e4b\u524d\u7684\u5185\u5b58\u7684\u4e00\u4e2a\u53e0\u52a0\u800c\u5df2\u3002\n\u53e6\u5916\uff0c\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u5b9e\u4f8b\u5316\uff0c\u901a\u8fc7\u4f7f\u7528\u548c8.10\u5c0f\u8282\u540c\u6837\u7684\u6280\u672f\uff0c\u63cf\u8ff0\u5668\u4fdd\u5b58\u4e86\u8be5\u5b9e\u4f8b\u4e2d\u7684\u5185\u90e8\u7ed3\u6784\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7684\u4fee\u6b63\u7248\uff0c\u4f60\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point(Structure):\n    _fields_ = [\n        ('<d', 'x'),\n        ('d', 'y')\n    ]\n\nclass PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        (Point, 'min'), # nested struct\n        (Point, 'max'), # nested struct\n        ('i', 'num_polys')\n    ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u5b83\u4e5f\u80fd\u6309\u7167\u9884\u671f\u7684\u6b63\u5e38\u5de5\u4f5c\uff0c\u6211\u4eec\u5b9e\u9645\u64cd\u4f5c\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min # Nested structure"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4e00\u4e2a\u5904\u7406\u5b9a\u957f\u8bb0\u5f55\u7684\u6846\u67b6\u5df2\u7ecf\u5199\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u7ec4\u4ef6\u8bb0\u5f55\u662f\u53d8\u957f\u7684\u5462\uff1f\n\u6bd4\u5982\uff0c\u591a\u8fb9\u5f62\u6587\u4ef6\u5305\u542b\u53d8\u957f\u7684\u90e8\u5206\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u79cd\u65b9\u6848\u662f\u5199\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u5b57\u8282\u6570\u636e\uff0c\u540c\u65f6\u5199\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\u6765\u901a\u8fc7\u591a\u5c11\u65b9\u5f0f\u89e3\u6790\u5185\u5bb9\u3002\u8ddf6.11\u5c0f\u8282\u7684\u4ee3\u7801\u5f88\u7c7b\u4f3c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SizedRecord:\n    def __init__(self, bytedata):\n        self._buffer = memoryview(bytedata)\n\n    @classmethod\n    def from_file(cls, f, size_fmt, includes_size=True):\n        sz_nbytes = struct.calcsize(size_fmt)\n        sz_bytes = f.read(sz_nbytes)\n        sz, = struct.unpack(size_fmt, sz_bytes)\n        buf = f.read(sz - includes_size * sz_nbytes)\n        return cls(buf)\n\n    def iter_as(self, code):\n        if isinstance(code, str):\n            s = struct.Struct(code)\n            for off in range(0, len(self._buffer), s.size):\n                yield s.unpack_from(self._buffer, off)\n        elif isinstance(code, StructureMeta):\n            size = code.struct_size\n            for off in range(0, len(self._buffer), size):\n                data = self._buffer[off:off+size]\n                yield code(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u65b9\u6cd5 SizedRecord.from_file() \u662f\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u6765\u4ece\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5e26\u5927\u5c0f\u524d\u7f00\u7684\u6570\u636e\u5757\uff0c\n\u8fd9\u4e5f\u662f\u5f88\u591a\u6587\u4ef6\u683c\u5f0f\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u4f5c\u4e3a\u8f93\u5165\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u5927\u5c0f\u7f16\u7801\u7684\u7ed3\u6784\u683c\u5f0f\u7f16\u7801\uff0c\u5e76\u4e14\u4e5f\u662f\u81ea\u5df1\u5f62\u5f0f\u3002\n\u53ef\u9009\u7684 includes_size \u53c2\u6570\u6307\u5b9a\u4e86\u5b57\u8282\u6570\u662f\u5426\u5305\u542b\u5934\u90e8\u5927\u5c0f\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u6559\u4f60\u600e\u6837\u4f7f\u7528\u4ece\u591a\u8fb9\u5f62\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5355\u72ec\u7684\u591a\u8fb9\u5f62\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.num_polys"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "polydata = [ SizedRecord.from_file(f, '<i')\n            for n in range(phead.num_polys) ]\npolydata"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u51fa\uff0cSizedRecord \u5b9e\u4f8b\u7684\u5185\u5bb9\u8fd8\u6ca1\u6709\u88ab\u89e3\u6790\u51fa\u6765\u3002\n\u53ef\u4ee5\u4f7f\u7528 iter_as() \u65b9\u6cd5\u6765\u8fbe\u5230\u76ee\u7684\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7ed3\u6784\u683c\u5f0f\u5316\u7f16\u7801\u6216\u8005\u662f Structure \u7c7b\u4f5c\u4e3a\u8f93\u5165\u3002\n\u8fd9\u6837\u5b50\u53ef\u4ee5\u5f88\u7075\u6d3b\u7684\u53bb\u89e3\u6790\u6570\u636e\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for n, poly in enumerate(polydata):\n    print('Polygon', n)\n    for p in poly.iter_as('<dd'):\n        print(p)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for n, poly in enumerate(polydata):\n    print('Polygon', n)\n    for p in poly.iter_as(Point):\n        print(p.x, p.y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u6240\u6709\u8fd9\u4e9b\u7ed3\u5408\u8d77\u6765\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a read_polys() \u51fd\u6570\u7684\u53e6\u5916\u4e00\u4e2a\u4fee\u6b63\u7248\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point(Structure):\n    _fields_ = [\n        ('<d', 'x'),\n        ('d', 'y')\n    ]\n\nclass PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        (Point, 'min'),\n        (Point, 'max'),\n        ('i', 'num_polys')\n    ]\n\ndef read_polys(filename):\n    polys = []\n    with open(filename, 'rb') as f:\n        phead = PolyHeader.from_file(f)\n        for n in range(phead.num_polys):\n            rec = SizedRecord.from_file(f, '<i')\n            poly = [ (p.x, p.y) for p in rec.iter_as(Point) ]\n            polys.append(poly)\n    return polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u8282\u5411\u4f60\u5c55\u793a\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5305\u62ec\u63cf\u8ff0\u5668\uff0c\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u5143\u7c7b\uff0c\u7c7b\u53d8\u91cf\u548c\u5185\u5b58\u89c6\u56fe\u3002\n\u7136\u800c\uff0c\u5b83\u4eec\u90fd\u4e3a\u4e86\u540c\u4e00\u4e2a\u7279\u5b9a\u7684\u76ee\u6807\u670d\u52a1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u5b9e\u73b0\u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u5f81\u662f\u5b83\u662f\u57fa\u4e8e\u61d2\u89e3\u5305\u7684\u601d\u60f3\u3002\u5f53\u4e00\u4e2a Structure \u5b9e\u4f8b\u88ab\u521b\u5efa\u65f6\uff0c\n__init__() \u4ec5\u4ec5\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u636e\u7684\u5185\u5b58\u89c6\u56fe\uff0c\u6ca1\u6709\u505a\u5176\u4ed6\u4efb\u4f55\u4e8b\u3002\n\u7279\u522b\u7684\uff0c\u8fd9\u65f6\u5019\u5e76\u6ca1\u6709\u4efb\u4f55\u7684\u89e3\u5305\u6216\u8005\u5176\u4ed6\u4e0e\u7ed3\u6784\u76f8\u5173\u7684\u64cd\u4f5c\u53d1\u751f\u3002\n\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u52a8\u673a\u662f\u4f60\u53ef\u80fd\u4ec5\u4ec5\u53ea\u5bf9\u4e00\u4e2a\u5b57\u8282\u8bb0\u5f55\u7684\u67d0\u4e00\u5c0f\u90e8\u5206\u611f\u5174\u8da3\u3002\u6211\u4eec\u53ea\u9700\u8981\u89e3\u5305\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5b9e\u73b0\u61d2\u89e3\u5305\u548c\u6253\u5305\uff0c\u9700\u8981\u4f7f\u7528 StructField \u63cf\u8ff0\u5668\u7c7b\u3002\n\u7528\u6237\u5728 _fields_ \u4e2d\u5217\u51fa\u6765\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u4f1a\u88ab\u8f6c\u5316\u6210\u4e00\u4e2a StructField \u63cf\u8ff0\u5668\uff0c\n\u5b83\u5c06\u76f8\u5173\u7ed3\u6784\u683c\u5f0f\u7801\u548c\u504f\u79fb\u503c\u4fdd\u5b58\u5230\u5b58\u50a8\u7f13\u5b58\u4e2d\u3002\u5143\u7c7b StructureMeta \u5728\u591a\u4e2a\u7ed3\u6784\u7c7b\u88ab\u5b9a\u4e49\u65f6\u81ea\u52a8\u521b\u5efa\u4e86\u8fd9\u4e9b\u63cf\u8ff0\u5668\u3002\n\u6211\u4eec\u4f7f\u7528\u5143\u7c7b\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u662f\u5b83\u4f7f\u5f97\u7528\u6237\u975e\u5e38\u65b9\u4fbf\u7684\u901a\u8fc7\u4e00\u4e2a\u9ad8\u5c42\u63cf\u8ff0\u5c31\u80fd\u6307\u5b9a\u7ed3\u6784\u683c\u5f0f\uff0c\u800c\u65e0\u9700\u8003\u8651\u4f4e\u5c42\u7684\u7ec6\u8282\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "StructureMeta \u7684\u4e00\u4e2a\u5f88\u5fae\u5999\u7684\u5730\u65b9\u5c31\u662f\u5b83\u4f1a\u56fa\u5b9a\u5b57\u8282\u6570\u636e\u987a\u5e8f\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4efb\u610f\u7684\u5c5e\u6027\u6307\u5b9a\u4e86\u4e00\u4e2a\u5b57\u8282\u987a\u5e8f(<\u8868\u793a\u4f4e\u4f4d\u4f18\u5148 \u6216\u8005 >\u8868\u793a\u9ad8\u4f4d\u4f18\u5148)\uff0c\n\u90a3\u540e\u9762\u6240\u6709\u5b57\u6bb5\u7684\u987a\u5e8f\u90fd\u4ee5\u8fd9\u4e2a\u987a\u5e8f\u4e3a\u51c6\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u989d\u5916\u8f93\u5165\uff0c\u4f46\u662f\u5728\u5b9a\u4e49\u7684\u4e2d\u95f4\u6211\u4eec\u4ecd\u7136\u53ef\u80fd\u5207\u6362\u987a\u5e8f\u7684\u3002\n\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u6709\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u7ed3\u6784\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class ShapeFile(Structure):\n    _fields_ = [ ('>i', 'file_code'), # Big endian\n        ('20s', 'unused'),\n        ('i', 'file_length'),\n        ('<i', 'version'), # Little endian\n        ('i', 'shape_type'),\n        ('d', 'min_x'),\n        ('d', 'min_y'),\n        ('d', 'max_x'),\n        ('d', 'max_y'),\n        ('d', 'min_z'),\n        ('d', 'max_z'),\n        ('d', 'min_m'),\n        ('d', 'max_m') ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u6211\u4eec\u63d0\u5230\u8fc7\uff0cmemoryview() \u7684\u4f7f\u7528\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u907f\u514d\u5185\u5b58\u7684\u590d\u5236\u3002\n\u5f53\u7ed3\u6784\u5b58\u5728\u5d4c\u5957\u7684\u65f6\u5019\uff0cmemoryviews \u53ef\u4ee5\u53e0\u52a0\u540c\u4e00\u5185\u5b58\u533a\u57df\u4e0a\u5b9a\u4e49\u7684\u673a\u6784\u7684\u4e0d\u540c\u90e8\u5206\u3002\n\u8fd9\u4e2a\u7279\u6027\u6bd4\u8f83\u5fae\u5999\uff0c\u4f46\u662f\u5b83\u5173\u6ce8\u7684\u662f\u5185\u5b58\u89c6\u56fe\u4e0e\u666e\u901a\u5b57\u8282\u6570\u7ec4\u7684\u5207\u7247\u64cd\u4f5c\u884c\u4e3a\u3002\n\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u6216\u5b57\u8282\u6570\u7ec4\u4e0a\u6267\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u4f60\u901a\u5e38\u4f1a\u5f97\u5230\u4e00\u4e2a\u6570\u636e\u7684\u62f7\u8d1d\u3002\n\u800c\u5185\u5b58\u89c6\u56fe\u5207\u7247\u4e0d\u662f\u8fd9\u6837\u7684\uff0c\u5b83\u4ec5\u4ec5\u662f\u5728\u5df2\u5b58\u5728\u7684\u5185\u5b58\u4e0a\u9762\u53e0\u52a0\u800c\u5df2\u3002\u56e0\u6b64\uff0c\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u5f88\u591a\u76f8\u5173\u7684\u7ae0\u8282\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6269\u5c55\u8fd9\u91cc\u8ba8\u8bba\u7684\u65b9\u6848\u3002\n\u53c2\u80038.13\u5c0f\u8282\u4f7f\u7528\u63cf\u8ff0\u5668\u6784\u5efa\u4e00\u4e2a\u7c7b\u578b\u7cfb\u7edf\u3002\n8.10\u5c0f\u8282\u6709\u66f4\u591a\u5173\u4e8e\u5ef6\u8fdf\u8ba1\u7b97\u5c5e\u6027\u503c\u7684\u8ba8\u8bba\uff0c\u5e76\u4e14\u8ddfNestedStruct\u63cf\u8ff0\u5668\u7684\u5b9e\u73b0\u4e5f\u6709\u5173\u3002\n9.19\u5c0f\u8282\u6709\u4e00\u4e2a\u4f7f\u7528\u5143\u7c7b\u6765\u521d\u59cb\u5316\u7c7b\u6210\u5458\u7684\u4f8b\u5b50\uff0c\u548c StructureMeta \u7c7b\u975e\u5e38\u76f8\u4f3c\u3002\nPython\u7684 ctypes \u6e90\u7801\u540c\u6837\u4e5f\u5f88\u6709\u8da3\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3001\u6570\u636e\u7ed3\u6784\u5d4c\u5957\u8fd9\u4e9b\u76f8\u4f3c\u529f\u80fd\u7684\u652f\u6301\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.13 \u6570\u636e\u7684\u7d2f\u52a0\u4e0e\u7edf\u8ba1\u64cd\u4f5c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5904\u7406\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u636e\u96c6\u5e76\u9700\u8981\u8ba1\u7b97\u6570\u636e\u603b\u548c\u6216\u5176\u4ed6\u7edf\u8ba1\u91cf\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u4efb\u4f55\u6d89\u53ca\u5230\u7edf\u8ba1\u3001\u65f6\u95f4\u5e8f\u5217\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u6280\u672f\u7684\u6570\u636e\u5206\u6790\u95ee\u9898\uff0c\u90fd\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 Pandas\u5e93 \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8ba9\u4f60\u5148\u4f53\u9a8c\u4e0b\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u6765\u5206\u6790\u829d\u52a0\u54e5\u57ce\u5e02\u7684\n\u8001\u9f20\u548c\u556e\u9f7f\u7c7b\u52a8\u7269\u6570\u636e\u5e93 \u7684\u4f8b\u5b50\u3002\n\u5728\u6211\u5199\u8fd9\u7bc7\u6587\u7ae0\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u62e5\u6709\u5927\u698274,000\u884c\u6570\u636e\u7684CSV\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Read a CSV file, skipping last line\nrats = pandas.read_csv('rats.csv', skip_footer=1)\nrats"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Investigate range of values for a certain field\nrats['Current Activity'].unique()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Filter the data\ncrew_dispatched = rats[rats['Current Activity'] == 'Dispatch Crew']\nlen(crew_dispatched)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Find 10 most rat-infested ZIP codes in Chicago\ncrew_dispatched['ZIP Code'].value_counts()[:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Group by completion date\ndates = crew_dispatched.groupby('Completion Date')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "len(dates)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Determine counts on each day\ndate_counts = dates.size()\ndate_counts[0:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Sort the counts\ndate_counts.sort()\ndate_counts[-10:]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u55ef\uff0c\u770b\u6837\u5b502011\u5e7410\u67087\u65e5\u5bf9\u8001\u9f20\u4eec\u6765\u8bf4\u662f\u4e2a\u5f88\u5fd9\u788c\u7684\u65e5\u5b50\u554a\uff01^_^"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Pandas\u662f\u4e00\u4e2a\u62e5\u6709\u5f88\u591a\u7279\u6027\u7684\u5927\u578b\u51fd\u6570\u5e93\uff0c\u6211\u5728\u8fd9\u91cc\u4e0d\u53ef\u80fd\u4ecb\u7ecd\u5b8c\u3002\n\u4f46\u662f\u53ea\u8981\u4f60\u9700\u8981\u53bb\u5206\u6790\u5927\u578b\u6570\u636e\u96c6\u5408\u3001\u5bf9\u6570\u636e\u5206\u7ec4\u3001\u8ba1\u7b97\u5404\u79cd\u7edf\u8ba1\u91cf\u6216\u5176\u4ed6\u7c7b\u4f3c\u4efb\u52a1\u7684\u8bdd\uff0c\u8fd9\u4e2a\u51fd\u6570\u5e93\u771f\u7684\u503c\u5f97\u4f60\u53bb\u770b\u4e00\u770b\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}